如何在启动"up"时恢复活动而不是重新启动.从操作栏 [英] How to resume activity instead of restart when going "up" from action bar

查看:63
本文介绍了如何在启动"up"时恢复活动而不是重新启动.从操作栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两项活动.说Activity AActivity B.
Activity A中,我单击一个按钮以启动Activity B,这是我用于此的代码:

Intent intent = new Intent(this, ActivityB.class);
this.startActivity(intent);


现在,我在Activity B中.在Activity B的on create方法上,启用向上按钮.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_news);

    getActionBar().setDisplayHomeAsUpEnabled(true); //Here//
}

在向上"按钮的事件句柄上,我有以下代码:

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


现在,我决定返回到Activity A,单击操作栏中的向上按钮,如下所示:

现在的问题是,当我单击向上按钮"时,它返回到Activity A,这很好,但是它重新启动.如何使其停止重新启动?我只希望它恢复.

当我使用硬件后退按钮时,它可以按预期工作.即它转到Activity A并恢复它而不是重新启动它.

我希望恢复活动,因为在该活动中,我在线下载了一些字符串,因此当用户从活动B->活动A 转到用户时,我不希望它继续重新下载数据.


编辑我的Activity B部分清单文件如下:

<activity
    android:name="com.example.android.ActivityB"
    android:label="@string/title_activity_view_news"
    android:parentActivityName="com.example.android.ActivityA">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.android.ActivityA" />
</activity>


编辑2 [答案] 好的,这是非常出乎意料的,但是在我的AndroidManifest.xml中的活动A"部分中,我必须添加以下划线. android:launchMode="singleTop",所以现在看起来像这样:

<activity
    android:name="com.example.android.ActivityA"
    android:label="@string/app_name" 
    android:launchMode="singleTop"> //**HERE**//
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

对此的解释在此处

解决方案

应该添加为答案而不是评论. >

https://stackoverflow.com/a/16147110/3286163

基本上,总结一下,除非您不使用

指定,否则android总是会重新创建活动

android:launchMode="singleTop"

注意:如果返回活动不在参考文献中提到的后排堆栈顶部,则它将不起作用.

对于其他任何人,如果发现有用的话,请在url中而不是我的答案中添加答案.

I have two activities. Say Activity A and Activity B.
From Activity A I click a button to launch Activity B This is the code I use for this:

Intent intent = new Intent(this, ActivityB.class);
this.startActivity(intent);


Now at this point, I am in Activity B. On the on create method of Activity B, I enable the up button.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_view_news);

    getActionBar().setDisplayHomeAsUpEnabled(true); //Here//
}

and on the event handle for the "up" button, i have this code:

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


Now I decide to go back to Activity A I click the up button in the action bar which looks like this:

Now the problem is, when I click the "up button", it goes back to Activity A which is fine, but it restarts it. How can make it stop restarting? I just want it to resume.

When I use my hardware back button, it works as expected. i.e it goes to Activity A and resumes it instead of restarting it.

I want my activity to resume because on that activity, I download some string online and so I don't want it to keep re downloading the data when a users goes from Activity B -> Activity A


Edit My manifest file for Activity B section looks like this:

<activity
    android:name="com.example.android.ActivityB"
    android:label="@string/title_activity_view_news"
    android:parentActivityName="com.example.android.ActivityA">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.android.ActivityA" />
</activity>


Edit 2 [Answer] Ok so, this was pretty unexpected but in my AndroidManifest.xml, in the Activity A section, I had to add the following line liner. android:launchMode="singleTop" so it now looks like this:

<activity
    android:name="com.example.android.ActivityA"
    android:label="@string/app_name" 
    android:launchMode="singleTop"> //**HERE**//
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

The explanation to this is given here

解决方案

Should've added as an answer instead of a comment.. but here you go so you can accept if you want so others can see if necessary.

https://stackoverflow.com/a/16147110/3286163

Basically, to summarize the android will always recreate the activity unless you specify it not to with

android:launchMode="singleTop"

Note: it will not work if the returning activity is not on the top of the back stack as mentioned in the reference.

For anyone else, please upvote the answer in the url instead of mine if you find this useful.

这篇关于如何在启动"up"时恢复活动而不是重新启动.从操作栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆