I have an application installed in my device. I am trying to launch this application A from the buttonclick of another application B using the following code:
Button buttonStart = (Button)findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClassName("co.abc.android.test",
"co.abc.android.test.Abc");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Following is my issue:
I launch application A
I press home button
From application B's button onclick, I launch app A again
- Press return button to exit application A, which was now launched from app B
- Eventhough I exit the application A on pressing return button, I am again taken to the main activity of app A, which I had launched initially.
On referring for this issue, I have read in many places that using Intent.FLAG_ACTIVITY_CLEAR_TOP would resolve this. But since I call app A's intent from a place in which I have no access to its context, it gives me the following error.
01-01 00:09:54.694: ERROR/AndroidRuntime(283): *** FATAL EXCEPTION IN SYSTEM PROCESS: WindowManagerPolicy
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:884)
at com.android.internal.policy.impl.LockScreen$DialerMethods.onTrigger(LockScreen.java:218)
at com.android.internal.widget.multiwaveview.Dialer$2.run(Dialer.java:366)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at com.android.server.wm.WindowManagerService$PolicyThread.run(WindowManagerService.java:752)
How can I resolve this issue, such that when I press the return button, I do not see the same activity(if launched before) again?
Any help is much appreciated.