简体   繁体   English

Android:按“后退”按钮时启动主要活动

[英]Android: Start main activity when pressing Back Button

I need help with this activity thing.. I have a main activity and when a button is clicked a new activity starts and the main activity finishes. 我需要有关此活动的东西的帮助。.我有一个主要活动,当单击按钮时,一个新的活动开始并且主要活动结束。 What I want is to start аgain the main activity when the back button is pressed in the second activity. 我想要的是在第二个活动中按下“后退”按钮时开始进行主要活动。 Or if you can tell me a another way to refresh the main activity when I turn back from the second, without adding a new activity in the stack. 或者,如果您可以告诉我另一种刷新主要活动的方法,则当我从第二个活动中转回时,无需在堆栈中添加新活动。 I will really appreciate your help. 非常感谢您的帮助。 Thank you :) 谢谢 :)

Ps: I need to tell you that my activity extends ActionBarActivity, not Activity so I'm not pretty sure how to override onBackPressed() if I need to. 附:我需要告诉你,我的活动扩展了ActionBarActivity,而不是Activity,所以我不太确定如何在需要时重写onBackPressed()。

You need to provide a up navigation by adding a parent activity. 您需要通过添加父活动来提供向上导航。 This also improves the overall navigation experience for the user. 这也改善了用户的整体导航体验。

See - http://developer.android.com/training/implementing-navigation/ancestral.html 参见-http://developer.android.com/training/implementing-navigation/ancestral.html

Intent newIntent = new Intent(context, Another.class);
    context.startActivity(newIntent, Intent.FLAG_ACTIVITY_CLEAR_TOP);
    finish();

you can start your your new activity from 'Main' using the above code. 您可以使用上述代码从“主要”开始新的活动。 The 'finish()' call would remove the Main Activity from the stack. 'finish()'调用将从堆栈中删除Main Activity。

Then in your new activity you can do this : 然后,在您的新活动中,您可以执行以下操作:

@Override
public void onBackPressed(){
Intent newIntent = new Intent(context, Main.class);
    context.startActivity(newIntent);
    finish();
}

暂无
暂无

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

相关问题 当按下返回按钮时,结果活动关闭主要活动 - Result activity closes main activity when pressing back button 在主活动中按“后退”按钮时如何退出应用程序? - how to make the appliction exit when pressing back button in the main activity? 当按下导航栏后退按钮时,Android活动将返回启动它而不是父活动的活动 - Android activity go back to activity that started it instead of parent activity when pressing navigation bar back button 在子活动中按下返回按钮后,Android主要活动的元素不响应 - Android main activity's elements do not respond after pressing the back button from child activity 在儿童活动->应用程序中的主要活动中按返回按钮? - Pressing back button in child activity-> main activity in app? ANDROID:按下后退按钮后的活动状态 - ANDROID: Activity state after pressing the back button 活动完成()和按下 android 中的后退按钮时出现问题 - problem in activity finish() and on pressing back button in android 按返回按钮时活动未发送结果 - activity not sending result when pressing back button 按下“ BACK”(返回)按钮时是否返回正常活动? - Returning to the proper activity when pressing the BACK button? 在第二个活动中按“后退”按钮时,返回到“主要”片段将使数据保持相同的片段 - Returning to Main fragment when pressing back button from second activity keeps data the same fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM