使用向上按钮从活动返回到片段

Ant*_*tnA -1 android android-layout android-fragments android-activity

在主要活动中,我创建了两个用作选项卡式菜单的片段.其中一个片段包含一个列表,其中列表项在单击时开始新活动.我想使用Up Action/Navigation按钮从新活动返回到片段(列表),但不显示任何列表,只显示空标签.我已将parent活动声明为manifest.xml中的主要活动.

当我使用手机的后退按钮时它工作正常.

List是在fragment的onViewCreated方法中创建的.

清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Main2Activity"
        android:parentActivityName=".MainActivity" >
    </activity>
</application>
Run Code Online (Sandbox Code Playgroud)

使用listView启动新活动的片段:

 public static class frag extends Fragment {

    private static final String ARG_SECTION_NUMBER =   "section_number";

    public frag() {
    }
    public static frag newInstance(int sectionNumber) {
        frag fragment = new rangingFrag();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);


        return rootView;
    }

    @Override
    public void onViewCreated(View rootView, Bundle savedInstanceState) {
        ListView listView = (ListView) rootView.findViewById(R.id.mobile_list);
        final ArrayAdapter adapter = new ArrayAdapter<>(getActivity().getApplicationContext(), R.layout.listview);
        listView.setAdapter(adapter);
        adapter.addAll(beacons);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                                            public void onItemClick(AdapterView<?> parent, View v,
                                                                    int position, long id) {
                                                String selectedText = (String) parent.getItemAtPosition(position);
                                                Bundle bundle = new Bundle();
                                                bundle.putString("param1", selectedText);
                                                Intent myIntent = new Intent(v.getContext(), Main2Activity.class);
                                                myIntent.putExtras(bundle);
                                                startActivity(myIntent);
                                            }
                                        }
        );

    } }
Run Code Online (Sandbox Code Playgroud)

小智 5

在您Activity想要在操作栏上保留后退箭头的位置onCreate,写入:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)

然后覆盖:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            break;
    }

    return true;
}
Run Code Online (Sandbox Code Playgroud)