按下主活动上的返回按钮会打开其他活动。

huangapple go评论84阅读模式
英文:

Press back button on main activity opens other activity

问题

在我的项目中,我从主活动转到活动A,然后从活动A转到活动B。从活动B中,使用工具栏上的主菜单返回到主活动。现在,当我按下返回按钮时,应用程序应该退出,但它重新打开了活动A。

英文:

In my project i move from main activity to activity A and from activity A to activity B. From activity B using home menu on toolbar i jump back to main activity. Now when i press back button application should exit but it opens the activity A again.

答案1

得分: 0

在开始下一个活动之前调用finish()方法,以使其从活动中移除。在此处查找更多详细信息和选项:链接

英文:

Call the finish() method before starting the next activity to have it removed from the activity. Find more details and options here.

答案2

得分: 0

你应该利用启动标志来管理后台堆栈中的活动。根据我理解的情况,我认为你需要使用 FLAG_ACTIVITY_CLEAR_TOP 来启动你的主页活动。

了解更多关于启动标志的信息:https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP。

此外,查看以下链接以获取有关在Android上管理活动后台堆栈的更多详细信息:https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks。

英文:

You should make use of the launch flags to manage your activities in the back stack. As far as I understood your scenario, I think you need to use FLAG_ACTIVITY_CLEAR_TOP for starting your main/home activity.

Read more about launch flags: https://developer.android.com/reference/android/content/Intent#FLAG_ACTIVITY_CLEAR_TOP.

Also, take a look this for more details on managing activity back stack on Android: https://developer.android.com/guide/components/activities/tasks-and-back-stack#ManagingTasks

答案3

得分: 0

以下是您要的翻译内容:

Kotlin写在您的MainActivity中:

override fun onBackPressed() {
    moveTaskToBack(true)
    exitProcess(-1)
}

Java写在您的MainActivity中:

@Override
void onBackPressed() {
    moveTaskToBack(true);
    exitProcess(-1);
}

希望它对您和我一样有效。

英文:

For Kotlin write this in your MainActivity :

override fun onBackPressed() {
    moveTaskToBack(true)
    exitProcess(-1)
}

For Java write this in your MainActivity :

@Override
void onBackPressed() {
    moveTaskToBack(true)
    exitProcess(-1)
}

Hope to it will work for you as good as for me

答案4

得分: -1

Variables:

boolean backactivity = true;

CODE:

public boolean onOptionsItemSelected(MenuItem item){
if(backactivity==true)
{
finishActivity(1);
backactivity=false;
}else
{
Intent homeIntent = new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory( Intent.CATEGORY_HOME );
homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
return true;
}

英文:

hey i write some code for you
Variables:

boolean backactivity = true;

CODE:

public boolean onOptionsItemSelected(MenuItem item){
        if(backactivity==true)
        {
            finishActivity(1);
            backactivity=false;
        }else
        {
            Intent homeIntent = new Intent(Intent.ACTION_MAIN);
            homeIntent.addCategory( Intent.CATEGORY_HOME );
            homeIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);
        }
        return true;
    }

huangapple
  • 本文由 发表于 2020年8月23日 02:54:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63539974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定