简体   繁体   English

如何使用活动显示的片段转到上一个活动

[英]How can I go to previous activity with the fragment the activity was showing

I have two activities, Activity A and Activity B. Now Activity A has 3 Fragments one of which launches activity B. When I press the action bar button it returns to activity A but shows the default Fragment . 我有两个活动,活动A和活动B。现在活动A有3个Fragments ,其中一个启动活动B。当我按操作栏按钮时,它返回活动A,但显示默认Fragment Am using navUtils.navigateupFromSameTask to go back to previous Activity . 我正在使用navUtils.navigateupFromSameTask返回上一个Activity

How can I have the latest Fragment the user used show instead of the default one that shows up when user launches app the first time? 我如何显示用户使用的最新Fragment ,而不是用户首次启动应用程序时显示的默认Fragment For example when app launches it shows Fragment one. 例如,当应用启动时,它会显示Fragment一。 when user goes to Fragment 2 and launches activity B from it I want to go back to the previous activity showing fragment 2 当用户转到Fragment 2并从中启动活动B时,我想回到显示片段2的上一个活动

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

in the onCreate method I have 在onCreate方法中

if (savedInstanceState == null) {
    fragmentManager = getFragmentManager();
    fragment = new RadioSound();
    selectItem(0);
}

then selectItem : 然后selectItem

 private void selectItem(int position) {

    // update the main content by replacing fragments
 activeFrag = position;
 if(position == 0){
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
 }
 else if(position == 1){
     if(conv == null)
     conv = new Conversations();
     fragmentManager.beginTransaction().replace(R.id.content_frame, conv).commit();
 } else{
     fragmentManager.beginTransaction().replace(R.id.content_frame, (new Preferences())).commit();
 }
    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(fragments[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

When you return from activity B to activity A you recreate your fragments, so it will always show your fragment1 instead of your desired fragment2. 当您从活动B返回活动A时,您将重新创建片段,因此它将始终显示fragment1而不是所需的fragment2。

Your problem is, that you trust on savedInstanceState to decide if your fragments should be recreated or not. 您的问题是,您信任saveInstanceState来决定是否应该重新创建您的片段。 However, onSaveInstanceState() is only called, when the activity gets destroyed, which will not instantly happen when you navigate to the next activity. 但是,仅在活动被销毁时才调用onSaveInstanceState() ,当您导航到下一个活动时,不会立即发生。

You should query the FragmentManager in your onCreate() method of activity A, if fragments are already added, otherwise you do your initial fragment creation. 如果已经添加了片段,则应该在活动A的onCreate()方法中查询FragmentManager ,否则将进行初始片段创建。 In order to do so, I would add an TAG to better find your fragments. 为了做到这一点,我将添加一个TAG以更好地找到您的片段。

private static final String FRAGMENT_TAG_CONVERSATION = "frag_conv";
...
fragmentManager.beginTransaction().replace(R.id.content_frame, conv, FRAGMENT_TAG_CONVERSATION).commit();

You can then check, if you already added the fragment: 然后,您可以检查是否已添加片段:

 if(fragmentManager.findFragmentByTag(FRAGMENT_TAG_CONVERSATION) == null))
     // only add it here

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM