Reload activity in Android

2019-01-03 19:47发布

Is it a good practice to reload an Activity in Android?

What would be the best way to do it? this.finish and then this.startActivity with the activity Intent?

14条回答
贼婆χ
2楼-- · 2019-01-03 20:25

Start with an intent your same activity and close the activity.

Intent refresh = new Intent(this, Main.class);
startActivity(refresh);//Start the same Activity
finish(); //finish Activity.
查看更多
Viruses.
3楼-- · 2019-01-03 20:27

for those who don't want to see that blink after recreate() method simply use

 finish();
 overridePendingTransition(0, 0);
 startActivity(getIntent());
 overridePendingTransition(0, 0);
查看更多
爷的心禁止访问
4楼-- · 2019-01-03 20:29

This is what I do to reload the activity after changing returning from a preference change.

@Override
protected void onResume() {

   super.onResume();
   this.onCreate(null);
}

This essentially causes the activity to redraw itself.

Updated: A better way to do this is to call the recreate() method. This will cause the activity to be recreated.

查看更多
Melony?
5楼-- · 2019-01-03 20:31

simply use

this.recreate();

this will trigger the onCreate method in the activity

查看更多
来,给爷笑一个
6楼-- · 2019-01-03 20:36

After experimenting with this for a while I've found no unexpected consequences of restarting an activity. Also, I believe this is very similar to what Android does by default when the orientation changes, so I don't see a reason not to do it in a similar circumstance.

查看更多
对你真心纯属浪费
7楼-- · 2019-01-03 20:36

I had another approach like: setting the launchMode of my activity to singleTop and without calling finish(), just startActivity(getIntent()) will do the job. Just have to care about the new data both in onCreate() and onNewIntent. With Sush's way, the application may blink like AMAN SINGH said. But AMAN SINGH's approach will still create a new activity which is somehow, unnecessary, even if he fixed the 'blink' problem, I think.

Too late for this question, but if someone looking for a solution, here it is.

查看更多
登录 后发表回答