开发者

Android: Service start Activity

开发者 https://www.devze.com 2023-02-13 02:18 出处:网络
The problem is: Service send me a data every 1 hour a开发者_如何学运维nd i need to start Activity1. If i do nothing, its all the time will create the same Activity1 and in a 5 hours in stack i will ha

The problem is: Service send me a data every 1 hour a开发者_如何学运维nd i need to start Activity1. If i do nothing, its all the time will create the same Activity1 and in a 5 hours in stack i will have 5 the same Activities...So, how to kill activity before new one will start ? Thanks !

            Intent dialogIntent = new Intent(getBaseContext(), someClass.class);
            getApplication().startActivity(dialogIntent);


So, how to kill activity before new one will start ?

You don't.

First, your users will attack you with hunting knives for popping up an activity in the middle of nowhere. You do not know what they may be in the middle of doing (playing a game, typing a text message, etc.). Only a very few apps, such as VOIP clients and alarm clocks, should be displaying activities except based upon direct user input.

Second, you don't "kill activity" from a service. Rather you put appropriate flags on your Intent to bring the existing activity to the front if it exists (e.g., FLAG_REORDER_TO_FRONT), rather than create a new one.


There is no need to kill an activity when you can just update the old one. Killing the activity and re-instantiating it will just result in unnecessary overhead.

Just write the code to reset your existing activity's state every time it gets a new Intent.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号