OnBack pressed执行带我回previous活动后, [英] OnBackPressed after implementation taking me back to a previous activity

查看:115
本文介绍了OnBack pressed执行带我回previous活动后,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android和学习全新的,我注意到 OnBack pressed 带您在游戏中的previous布局,现在我说这code这在 OnBack pressed

I am totally new in Android and learning,and i noticed OnBackPressed takes you to the previous layout in the game, now i added this code which closes the app at OnBackPressed

@Override
    public void onBackPressed() {
        Intent homeIntent = new Intent(Intent.ACTION_MAIN);
        homeIntent.addCategory( Intent.CATEGORY_HOME );
        homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(homeIntent);
        super.onBackPressed();
    }

我的问题是,当你重新开始游戏需要你回到previous布局不是主要的布局。举个例子,你有4个活动,当我开始游戏就带我去活动3我该如何避免这种情况?

My problem is when you start the game again it takes you back to the previous layout not the main layout. Take for example you have 4 activities, when i start the game it takes me to activity 3 how can i avoid this?

<application
    android:allowBackup="true"
    android:icon="@mipmap/icon1"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".StartGame"
        android:label="@string/app_name"
        android:screenOrientation="portrait"/>

    <activity
        android:name=".MathQuestions"
        android:label="@string/app_name"
        android:screenOrientation="portrait"
        />

        <activity android:name=".HighScores"
            android:label="@string/app_name"
            android:configChanges="keyboardHidden|orientation|screenSize"/>
    <activity android:name=".HowToPlay" >

    </activity>
</application>

推荐答案

修改标记:

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);  

和调用,而不是super.onBack pressed()完成();

and call finish() instead of super.onBackPressed();

我做了一些阅读和发现这一点:

I did some reading and found this:

此次发布的模式也可以用来配合良好的效果与FLAG_ACTIVITY_NEW_TASK:如果用来启动任务的根系活力,就会带来这个任务的任何当前正在运行的实例推到前台,然后清除它,以它的根州。这是特别有用的,例如,从通知管理器启动一个活动时

This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager.

这是为CLEAR_TOP文档

From documentation for CLEAR_TOP

编辑:另一种解决方案。

Another solution.

嗯,这是不是为你工作,让我们做这种方式:

Well this isn't working for you, let's do it this way:

创建扩展应用程序的自定义类,则不需要此的大部分时间,但会有所帮助。说它MyApp.java

Create a custom class that extends application, you don't need this most of the time but will help here. Call it MyApp.java

public class MyApp extends Application {
    private HashSet<Activity> mActivities;

@Override
public void onCreate(){
    super.onCreate();
    mActivities = new HashSet<Activity>();
}

public void addActivity(Activity activity){
    if(!mActivities.contains(activity))
        mActivities.add(activity);
}

public void removeActivity(Activity activity){
    if(mActivities.contains(activity))
        mActivities.remove(activity);
}
public void close(){
    for(Activity activity : mActivities){
        activity.finish();
    }
}

和它添加到应用程序中的标记你的Andr​​oid清单(保持休息,下同):

And add this to your android manifest in the application tag (keep rest the same):

<application
    android:name=".MyApp"
    ...
    ...

现在,在每一个活动请拨打以下:

Now, in every activity call the following:

@Override
public void onStart(){
    super.onStart();
    ((MyApp) getApplication()).addActivity(this);
}

所以,现在的每一次活动都存储在自己的HashSet可当你需要它是可以正常完成。

So now every activity is stored in your own HashSet can be gracefully finished when you need it to be.

而不是调用的意图,你现在只是做的:

Instead of calling the intent, you would now just do:

@Override
public void onBackPressed(){
    ((MyApp) getApplication()).close();
}

所有活动完成后,释放资源,当你回来的应用程序将是你的家庭活动。现在,你可能/可能不需要removeActivity要求,其中一项活动被破坏或停止的情况下,但这种方法应该工作。就像我说的,这是一个多一点的工作,但可以解决你的问题。

All activities are finished, resources released and when you come back to the app it will be at your Home Activity. Now, you may/may not need the removeActivity call for cases where an activity is destroyed or stopped, but this solution should work. Like I said, it's a little more work but would solve your problem.

这篇关于OnBack pressed执行带我回previous活动后,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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