Reputation: 4659
I'm looking for an answer to the question where on an event i want to open two activity parallely
where one of them will be in background and another in foreground but when the foreground activity stops the background activity should fire up.
Is it possible? I would like to know the trick
@downvoters kindly comment if you find it useless
Update
Here is the code that I have tried:
Intent intent = new Intent(context, ActivityTwo.class);
Intent home= new Intent(context,MainActivity.class);
Intent[] intents={intent,home};
PendingIntent pendingIntent = PendingIntent.getActivities(context,0,intents,0);
But this is opening only the MainActivity
class.
where i want it to open the Activitytwo
class in the foreground and mainActivity
in the background.
SOLVED
For those who wish to know how to open multiple activities parallel from widget OK here is my problem
The string array it should be
Intent[] intents={home,intent};
instead of
Intent[] intents={intent,home};
Upvotes: 1
Views: 1307
Reputation: 2130
Its possible with startActivities(Context, Intent[], bundle). Only Argument here to be explained is Intent[], which is array of intents of all the activities you need to fire.
Only one Activity (not sure the first or last intent in array, you can experiment on this) , will stay in foreground and remaining will stay in background. When you press back, they will sequentially keep firing until launcher activity.
From docs:
public static boolean startActivities (Context context, Intent[] intents, Bundle options)
Start a set of activities as a synthesized task stack, if able.
In API level 11 (Android 3.0/Honeycomb) the recommended conventions for app navigation using the back key changed. The back key's behavior is local to the current task and does not capture navigation across different tasks. Navigating across tasks and easily reaching the previous task is accomplished through the "recents" UI, accessible through the software-provided Recents key on the navigation or system bar. On devices with the older hardware button configuration the recents UI can be accessed with a long press on the Home key.
When crossing from one task stack to another post-Android 3.0, the application should synthesize a back stack/history for the new task so that the user may navigate out of the new task and back to the Launcher by repeated presses of the back key. Back key presses should not navigate across task stacks.
startActivities provides a mechanism for constructing a synthetic task stack of multiple activities. If the underlying API is not available on the system this method will return false.
Parameters context Start activities using this activity as the starting context intents Array of intents defining the activities that will be started. The element length-1 will correspond to the top activity on the resulting task stack. options Additional options for how the Activity should be started. See {@link android.content.Context#startActivity(Intent, Bundle) Returns true if the underlying API was available and the call was successful, false otherwise
UPDATE
The element length-1 will correspond to the top activity on the resulting task stack.
so this resolves my confusion too!
UPDATE
Intent intent = new Intent(context, ActivityTwo.class);
Intent home= new Intent(context,MainActivity.class);
Intent[] intents={home,intent};
startActivities(context, intents, null);
Upvotes: 2
Reputation: 541
Intent intent = new Intent(SplashActivity.this, FirstActivity.class);
startActivityForResult(intent, 10);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Intent intent = new Intent(SplashActivity.this, SecondActivity.class);
startActivity(intent);
}
in this example second activity will opened after first activity closed.
Upvotes: 0