Start multilple activity from broadcastReceiver - android

I want to start multiple activities from my broadcast receiver. I have two classes i.e ReadContacts and CallDetails. I want to start them one by one. like first calldetails activity should be started and then next. I have tried below code and it works fine.
Intent calldetails = new Intent();
calldetails.setClassName("com.simplereader", "com.simplereader.Calldetails");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(calldetails);
then I tried below code to start other activity
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(readcontacts);
But its not working and application crashes.

You must have the Intent Flag Intent.FLAG_ACTIVITY_NEW_TASK to start an Activity from outside of an Activity context so you need to add that flag to your second Intent.
I don't know if this is your only problem but if that doesn't fix it then post your logcat so we can see the error.
Intent readcontacts = new Intent();
readcontacts.setClassName("com.simplereader", "com.simplereader.ReadContacts");
calldetails.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // you need this flag
context.startActivity(readcontacts);

FLAG_ACTIVITY_MULTIPLE_TASK Do not use this flag unless you are implementing your own top-level application launcher.
From the android developer documentation for intent.
You could probably just launch both activities with the new task flag.

I think you are making mistake in this line
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
If you want to start readcontacts activity it shoul be
readcontacts.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
instead of
calldetails.setFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
I think this is the reason.

Related

How to make sure the MainActivity will be created only once

I have a react native project. And there is a service running in the background.
When the app is not running, the service is still alive, and it may want to start the MainActivity in some time. I’m using the following code to start MainActivity(I tried noFlag/addFlag/setFlag):
Intent Intent = new Intent(context, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(key, value);
context.startActivity(intent);
The AndroidManifest.xml is declared like:
<activity
android:name=".MainActivity"
android:launchMode="singleTask”>
</activity>
Each time the MainActivity will be created twice. In the first time we can get the extra value, but the second time it will be empty.
How can I make sure the MainActivity will only be created once?
Thanks.
Just found the problem. My method breakpoint on startActivity worked.
The third party sdk cannot find property receiver, so it send a startActivity call with intent flag FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TASK, which overrode my singleTask in AndroidManefest.xml.
As you most probably have known, the problem lies with you calling the startActivity twice. I think it would be lovely if you could show the code that calls the startActivity.
I have just now tested that as long as you have singleTask or singleTop as your launchmode, OnCreate wont be called twice. As you can read from here : https://developer.android.com/guide/components/activities/tasks-and-back-stack
the function that would be called is onNewintent and if the putExtra is empty, it indeed can create a NPE
I hope that helps you, Good luck

FLAG_ACTIVITY_NEW_TASK when you create an exit button

I was reading this question because I want to create an exit button for my android app. I found the following answer
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but I don't understand why he set the flag Intent.FLAG_ACTIVITY_NEW_TASK. What exactly this flag is gonna do? Can I omit it? Which is the best approach?
By the way I got confused when I read
the current task will simply be brought to the front of the screen
with the state it was last in
from the official documentation (here). I thought that FLAG_ACTIVITY_NEW_TASK will create a new back-stack and hence lose the state it was last in...
This flag is required when you start an activity from outside of an activity,like service or another activity outside the current app. It is actually used for making another instance of the activity as a new task.Remove the flag and it should work. if it does not, try using flag Single top.If you are planning to exit from the app the flag is not required.you can remove it.

Handling Previous Activities

I have a spinner that essentially starts multiple new activities based on onItemSelect();
However, what's happening on my app is if I start the same activity multiple times I have to hit the Android back button multiple times. How do I start the same activity and kill the previous one so that I don't have multiple layouts sitting there open?
Set android:noHistory=true for your <activity> in Manifest.xml. See here
Or, programatically:
Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Use the FLAG_ACTIVITY_CLEAR_TOP activity flag.
When you start a new activity you need to finish your old activity. So when you call your new intent:
startActivity(intent);
finish();
That will end the current activity.
You might want to consider setting the activity launchmode to "singletop" (this can be done in the Android Manifest). Then instead of creating a new activity, it will call OnNewIntent() of the existing activity.

Resume activity from broadcast receiver

I'm trying to resume an activity from within a broadcast receiver's onReceive() method as follows:
Intent i = new Intent(context, TimerSet.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
However the activity (TimerSet.class) is recreated instead of resumed. The only recommended solution I found to this problem was to use the FLAG_ACTIVITY_REORDER_TO_FRONT but I'm already using it.
Also, using Intent.FLAG_ACTIVITY_NEW_TASK doesn't fit my use case but I get the following exception when I do not provide it:
android:util.AndroidRuntimeException: Calling startActivity() from outside of an
Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you
want?
I am not sure whether this is exactly your problem or not, but I have a situation where I got a notification and I want to start my app without starting a new instance (if it's already running)
I finally figured out that these will work. The FLAG_ACTIVITY_NEW_TASK will not start a new instant if the activity has already been running. However, it will add it to the existing stack. Therefore, we can do a FLAG_ACTIVITY_CLEAR_TOP, so back will bring user to the home screen but not the previous state.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
remove FLAG_ACTIVITY_NEW_TASK flag.
also add this flag ->FLAG_ACTIVITY_CLEAR_TOP. This would prevent new activity to be created if already present.

Start Multiple Instances of an activity from BroadcastReceiver

I want to create multiple instances of an activity from BroadcastReceiver, the activity contains a AlertDialog, currently I am using the following code for this purpose:
Intent intent = new Intent(this, MultipleInstanceActivity.calss);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
and in manifest file android:launchMode="standard" somehow I think this FLAG_ACTIVITY_NEW_TASK causing the android:launchMode="standard" to change to android:launchMode="singleInstance" or something. I am not able to create multiple instances of this activity. I also tried to use FLAG_ACTIVITY_MULTIPLE_TASK, but no use.
I have created a PreferenceActivity, what really puzzles me is that when this PreferenceActivity is open my app creates multiple dialogbox i.e multiple instances with different data on it. But when its not open, my app wont create multiple instances just to make clear, it wont open another dialog. Logcat is not giving any warnings or errors.
My questions:
How to create multiple instances of an activity from BroadcastReceiver?
Can someone explain me whats happening in the second case, the PreferenceActivity one, why is it creating multiple instances?
I ran into the same problem as you have here, and i solved it by Using both FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
Intent intent = new Intent(context, YourActivityClass.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(intent);
Hope this works for you too.

Categories

Resources