[英]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.