我知道有些人问过这个问题,但没有答案对我有帮助,而且我是android新手:)
This answer可以帮助我。但我无法理解。
我想要自动刷新当我使用偶数物理后退按钮返回Activity1时。
活动代码1
public class Activity1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
}
});
}
}
活动代码2
public class Activity2 extends Activity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
}
}
请求详细的建议...... 在此先感谢。
答案 0 :(得分:2)
你应该能够覆盖onResume并在那里做你需要的事情。它应该在您的活动首次启动或重新获得焦点时调用。
请点击此处了解详情:http://developer.android.com/training/basics/activity-lifecycle/starting.html。
答案 1 :(得分:1)
由于您正在调用startActivityForResult(myIntent, 0)
来触发第二个Activity,当该Activity完成时,无论触发Activity2完成什么操作,您的第一个Activity都会收到回调onActivityResult()
。
public class Activity1 extends Activity {
/** ...The code you already wrote here... **/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
//Do your update or refresh here...
}
@Override
protected void onResume() {
super.onResume();
//This method also gets called every time the Activity is
// moved into the foreground, you can call your refresh here as well.
}
}
如果您需要传回一些数据,可以在完成之前在第二个活动中致电setResult()
,这些数据将在onActivityResult()
resultCode
和intent
中提供参数。
此开发人员培训文章有望提供进一步的见解: http://developer.android.com/training/basics/intents/result.html