我想从状态栏通知中启动一个Activity A,如果Activity A已经在前台,则我想结束该Activity并重新启动Activity A。我该怎么做?
我想从状态栏通知中启动一个Activity A,如果Activity A已经在前台,则我想结束该Activity并重新启动Activity A。我该怎么做?
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
}
// This is inside Activity A
Intent i = new Intent(this, ActivityB.class);
startActivity();
finish(); // This will be called right after 'Activity B' finishes
请查看有关创建状态栏通知的文档。这绝对涵盖了使用Intent和PendingIntent从通知中启动Activity。
http://developer.android.com/guide/topics/ui/notifiers/notifications.html
如果Activity已经在运行,结束它并重新启动...我不确定这是否可以轻松完成,这取决于您真正想要的内容。您可以尝试在清单文件中使用启动模式Activity参数来实现某些功能:
http://developer.android.com/guide/topics/manifest/activity-element.html#lmode
然后让您的活动做出响应(最可能是使用onNewIntent()),并在程序上“重置”自己。可能会使用类似以下代码: