Reload activity in Android

匿名 (未验证) 提交于 2019-12-03 01:33:01

问题:

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?

回答1:

You can Simply use

finish(); startActivity(getIntent());

to refresh an Activity from within itself.



回答2:

This is what I do to relad 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.



回答3:

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:

I needed to update a message list in one of my applications in a hurry, so I just performed a refresh of my main UI activity before I closed the dialog I was in. I'm sure there are better ways to accomplish this as well.

// Refresh main activity upon close of dialog box Intent refresh = new Intent(this, clsMainUIActivity.class); startActivity(refresh); this.finish(); //


回答5:

Android includes a process management system which handles the creation and destruction of activities which largely negates any benefit you'd see from manually restarting an activity. You can see more information about it at Application Fundamentals

What is good practice though is to ensure that your onPause and onStop methods release any resources which you don't need to hold on to and use onLowMemory to reduce your activities needs to the absolute minimum.



回答6:

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.


回答7:

i have same problem

import android.support.v4.content.IntentCompat;  intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);

this code work for me . Android api 17



回答8:

in some cases it's the best practice in other it's not a good idea it's context driven if you chose to do so using the following is the best way to pass from an activity to her sons :

    Intent i = new Intent(myCurrentActivityName.this,activityIWishToRun.class);         startActivityForResult(i, GlobalDataStore.STATIC_INTEGER_VALUE);

the thing is whenever you finish() from activityIWishToRun you return to your a living activity



回答9:

I saw earlier answers which have been given for reloading the activity using Intent. Those will work but you can also do the same using recreate() method given in Activity class itself.

Instead of writing this

// Refresh main activity upon close of dialog box

Intent refresh = new Intent(this, clsMainUIActivity.class); startActivity(refresh); this.finish();

This can be done by writing this only

recreate();


回答10:

I don't think that's a good idea... it'd be better to implement a cleaner method. For instance, if your activity holds a form, the cleaner method could just clear each widget in the form and delete all temporary data. I guess that's what you want: restore the activity to its initial state.



回答11:

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.



回答12:

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.



转载请标明出处:Reload activity in Android
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!