开发者

Start new activity from notification in android

开发者 https://www.devze.com 2023-02-28 03:21 出处:网络
I want to start a Activity A from status bar notification, When the a开发者_运维问答ctivity A is already in front then i want to finish that and fresh start activity A. How can i do this?Review the do

I want to start a Activity A from status bar notification, When the a开发者_运维问答ctivity A is already in front then i want to finish that and fresh start activity A. How can i do this?


Review the documentation on creating Status Bar Notifications. This definitely covers starting and Activity from a Notification using an Intent and PendingIntent.

http://developer.android.com/guide/topics/ui/notifiers/notifications.html

As for if the Activity is already running, finish it and start it freshly... I'm not sure that can be done easily, depending on what you really want. You may be able to do something with the launch mode activity parameter in the manifest:

http://developer.android.com/guide/topics/manifest/activity-element.html#lmode

And then have your activity respond (with onNewIntent() most likely) and "reset" itself programmatically. Possibly with something like this:

Android restart my activity


You mean re-start Activity A? While the most common approach is just to re-launch a new Intent with your same class I think it uses way too memory. I'd rather create an "init" method which should be called from the onCreate AND when you want to re-launch your activity. Example:

public void onCreate(Bundle si){
    // Call super and set your layout...
    init();
}

/**
 * This method should be called whenever you want to restart your activity. The
 * biggest advantage is you already have your layout (setContentView() method) 
 */
private void relaunchActivityA(){
    // Clean or save anything you need to clean or save
    init();
}

private void init(){
    // Init your variables, threads, and so on
}

If you wrote 'finish that and fresh start Activity A' instead of 'Activity B', then right after your startActivity() -on activity A- call 'finish'. Example:

// This is inside Activity A
Intent i = new Intent(this, ActivityB.class);
startActivity();
finish(); // This will be called right after 'Activity B' finishes
0

精彩评论

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