【问题标题】:Android from Fragment to Activity, back press dont go back to fragemtAndroid 从 Fragment 到 Activity,后按不会回到 Fragment
【发布时间】:2015-07-25 07:07:19
【问题描述】:

我正在从我的片段加载一个活动。如果我按下设备上的后退按钮,它会返回到 Gridfragment(如预期的那样)。

但是,如果我单击操作栏返回箭头,它会返回到 mainActivity 托管片段 = HomeFragment 而不是 Gridfragment

来自 HomeFragment -(默认片段)

Fragment f = GridFragment.newInstance(GridFragment.TAG_bundlePhotosList);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, f).addToBackStack(null).commit();

片段网格:

Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
i.putExtra("from_grid", "from_grid");
tartActivity(i);

ImagePreviewActivity:

//Display back arrow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle(ImagePreviewActivity);

【问题讨论】:

  • 请输入显示您如何处理 onOptionsItemSelected(item) 的代码

标签: java android android-fragments android-intent android-actionbar


【解决方案1】:

This is the same problem, and as such the same solution as here.

当您使用向上导航时,会重新创建上一个活动。为了防止在您保留 UP 导航时发生这种情况,您可以获取父 Activity 的 Intent,如果存在则将其置于最前面,否则创建它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多