Android activity restart

╄→гoц情女王★ 提交于 2019-11-27 19:00:53

问题



I am having a confusion in restarting an activity.. I have two function that works well for the same task. Please guide me which is best and why?

public void restart()   
    {  
        Intent intent = getIntent();  
        overridePendingTransition(0, 0);  
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);  
        finish();  
        overridePendingTransition(R.anim.fade,R.anim.fade);
        startActivity(intent);

    }

or

public void restart()   
    {         
        onCreate();  
    }  

Thanks In advance?


回答1:


I think this is a cleaner way for your requirement.

    Intent intent = getIntent();
    overridePendingTransition(0, 0);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    finish();
    overridePendingTransition(0, 0);
    startActivity(intent);



回答2:


Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.

for more info see Activity




回答3:


This has been posted before:

Intent intent = getIntent();
finish();
startActivity(intent);


来源:https://stackoverflow.com/questions/6134249/android-activity-restart

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