Android的 - 导航上,从活动到碎片碎片、Android

由网友(道不尽凉世)分享简介:我开发了一些应用程序,我有一个问题。我有:1.活动A(抽屉式导航模式)​​与ListFragment中的FrameLayout:XML:<的FrameLayout...>< /的FrameLayout><的LinearLayout...>< / LinearLayout中&...

我开发了一些应用程序,我有一个问题。

我有: 1.活动A(抽屉式导航模式)​​与ListFragment中的FrameLayout: XML:     

 <的FrameLayout
        ...>

    < /的FrameLayout>

    <的LinearLayout
        ...>

    < / LinearLayout中>

< /android.support.v4.widget.DrawerLayout>
 

在活动B,其显示ListFragment的ListView的详细数据。

我怎么能回去(使用导航向上按钮)从b活动到活动A和保存ListFragment的UI(活动重新创建,如果我回去用家返回)。 顺便说一句,如果我preSS在我的手机的返回按钮,活动不重新创建和previous状态恢复。

解决方案

在使用上定位,那么previous活性重建。为prevent这种情况的发生,而你preserve向上的导航,你可以得到父活动的意图,并把它前面,如果它存在,否则,如果没有创建它。

 公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例android.R.id.home:
            意图parentIntent = NavUtils.getParentActivityIntent(本);
            parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(parentIntent);
            完();
            返回true;
    }
    返回super.onOptionsItemSelected(项目);
}
 
Android应用中10大导航设计错误

我还指定了 launchMode =singleTop在清单中。但我不知道这是有必要的。

I'm developing some application and I have one problem.

I have : 1. Activity A (Navigation Drawer pattern) with ListFragment in FrameLayout: xml:

    <FrameLayout
        ...>

    </FrameLayout>

    <LinearLayout
        ...>

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>

Activity B which shows the detail data of ListView in ListFragment.

How can I go back (using Navigation Up Button) from activity B to Activity A with saving UI of the ListFragment (Activity re-creates if I go back using Home Back). Btw, if I press the back button on my phone, activity does not re-create and returns in previous state.

解决方案

When you use UP navigation, then the previous activity is recreated. To prevent that from happening while you preserve the UP navigation, you can get the intent of the parent activity, and bring it to front if it exists, otherwise create it if not.

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            Intent parentIntent = NavUtils.getParentActivityIntent(this);
            parentIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(parentIntent);
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

I also specified launchMode="singleTop" in the Manifest. but I am not sure if that was necessary.

阅读全文

相关推荐

最新文章