简体   繁体   English

返回活动的片段

[英]Return to a activity's fragment

I have an activity (MainActivity) which has a navigation drawer and can show 2 fragments (Fragments A and B), one at a time. 我有一个活动(MainActivity),它具有一个导航抽屉,并且可以显示2个片段(片段A和B),一次显示一个。 (This activity is the default activity with navigation drawer created by android studio) (此活动是android studio创建的带有导航抽屉的默认活动)

When I choose fragment B on the drawer the action bar menu is updated to show a button specific for fragment B (Button P). 当我在抽屉上选择片段B时,操作栏菜单将更新为显示特定于片段B的按钮(按钮P)。

Button P open an independent activity (IndependentActivity) with an explicit intent, on this activity I perform a database operation and after it I finish this activity to go back to MainActivity. 按钮P以明确的意图打开一个独立的活动(IndependentActivity),对此活动我执行数据库操作,完成此活动后,请返回MainActivity。

The problem is: When IndependentActivity is finished, MainActivity is shown but it shows fragment A instead of fragment B which was the one that called the intent to go to IndependentActivity. 问题是:完成IndependentActivity后,将显示MainActivity, 它显示的是片段A而不是片段B,后者是调用意图转到IndependentActivity的片段。

How do I fix this by showing the fragment that initiated the action to go to another activity? 如何通过显示引发该动作进行另一个活动的片段来解决此问题? Is there any way to save the fragment that was appearing? 有什么方法可以保存出现的片段?

Basically what you have to do is: 基本上,您要做的是:

  • Save the state of the MainActivity when you enter IndependentActivity 输入IndependentActivity时,保存MainActivity的状态
  • Reload the state of MainActivity when you exit IndependentActivity 退出IndependentActivity时重新加载MainActivity的状态

A simple implementation of this could be: 一个简单的实现可以是:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save which fragment that is in the view when entering another activity
    savedInstanceState.putString("fragment", "fragmentB");
    super.onSaveInstanceState(savedInstanceState);
}

Fragment B can be replaced with a string that tells the application which is the current fragment. 片段B可以用一个字符串替换,该字符串告诉应用程序当前片段。

Once you come back to the activity you want to: 回到活动之后,您要:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if( savedInstanceState != null ) {
         // Get which fragment that was active when you left the activity
         savedInstanceState.getString("fragment");

         // Programatically select the fragment here
    }
}

You can read more about saving instance state here: 您可以在此处阅读有关保存实例状态的更多信息:

http://developer.android.com/training/basics/activity-lifecycle/recreating.html http://developer.android.com/training/basics/activity-lifecycle/recreating.html

I will leave it to you to programmatically select the fragment. 我将留给您以编程方式选择片段。 I hope it helps! 希望对您有所帮助!

I found out that I was having the same problem as this: https://stackoverflow.com/a/29464116/2325672 我发现我遇到了与此相同的问题: https : //stackoverflow.com/a/29464116/2325672

The back arrow to return did not work and reset the activity's fragments and the emulator back triangle worked perfectly. 返回的后退箭头不起作用,因此重置了活动的片段,并且模拟器的后三角效果很好。

Adding this code to IndependentActivity worked for me: 将此代码添加到IndependentActivity可以为我工作:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId()== android.R.id.home) {
        Intent intent = NavUtils.getParentActivityIntent(this);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        NavUtils.navigateUpTo(this, intent);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

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