Hi I have a Broadcast receiver with following code.
1. Intent i = new Intent(context, A.class);
2. i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
3. context.startActivity(i);
This works fine and my activity does start, but it starts on top of my main activity i.e. B. What I want is that from my broadcast receiver I should be able start an Activity A, such that it does not start on top of B. Why B is always starting in background. What I am doing wrong.
Also, to mention by activity A has Theme.Dialog.
Please let me know your thoughts.
A new activity is supposed to start on top of existing activities.
If you want to start something without appearing in the activity history stack, maybe this something shouldn't be an Activity but a Service instead?
Edit: I think you can change this behaviour with FLAG_ACTIVITY_NEW_TASK in conjunction with android:launchMode="singleTask" in the Manifest. (see documentation on launch mode)
If you start an Activity then of course it will come foreground.The activity previously was in foreground will go to the background automatically.
If you don't need to start it on top of B,what about to start it from service?.Let Activity A stay as it is.
If you need to access anything from service and no need to show the activity,then surely you are looking for other then starting an Activity with an intent.
Finally if you really want to start Activity A,and B will be on top then start Activity B after starting A
I think you mean you want to start the application with a different activity. You can edit your AndroidManifest.xml file to make a different activity start first.
EDIT: Heres an example:
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".MainActivity" <-this will start first
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name =".otherActivity"
android:theme="#android:style/Theme.Dialog">
</activity>
<activity android:name=".anotherActivity"></activity>
</application>
Related
I have this situation in my app: I have these activities
<activity
android:name=".presentation.view.start.view.activity.StartActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".presentation.view.main.view.activity.MainActivity"
android:configChanges="locale"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppThemeMultiStep"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".presentation.view.firstScreen.view.activity.FirstScreenActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppThemeMultiStep"
android:windowSoftInputMode="stateAlwaysHidden" />
<activity
android:name=".presentation.view.signup.view.activity.LoginViaPinActivity"
android:screenOrientation="sensorPortrait"
android:theme="#style/AppThemeMultiStep" />
StartActivity acts as a sort of "router" based on the application state:
If I am not logged in, it will launch FirstScreenActivity
If I am logged in, it will launch LoginViaPinActivity, which will login the user based on some logic and then launch MainActivity. At the end of all, MainActivity will be at the root of the activities stack.
At some point the app will receive a notification, and when I tap it I want this:
if the app is running and MainActivity is running, open MainActivity (this is easy, there are planty of ways I can to that with various flags) but if it's not running launch StartActivity (so that I can open the app based on all the startup logics implemented there).
So the options I can think of are:
know if an activity is running in order to fire an intent or another (I don't like static fields solutions like you read in many SO post related to this)
make StartActivity the root of the task and find a combination of intent flags which will act like "launch StartActivity, but if it is running at the root of a task, bring that task to front" (this would be my preferred option if possible)
any suggestion is very appreciated
How do you usually handle this kind of situations? (I don't think I'm the first in the world with this problem :) )
Here is my approach -
Make StartActivity as your router as you've said. Just make launchmode to singleTop in your manifest in order to user onNewIntent() method of an Activity.
You'll generate notification, sending some data with contentIntent - as a result clicking on notification you'll be redirected to StartActivity.
Two cases here -
If StartActivity is in stack
onNewIntent gets called with your new Intent - Choose your activity required to be open - If it is already in stack Bring it to front using FLAG_ACTIVITY_REORDER_TO_FRONT flag
If StartActivity is not running or not in stack
Receive bundle of intent that is being got from the intent via notification, parse data and open an activity accordingly.
My application have two activities, say MainActivity and SecondActivity. Main activity is declared as android:launchMode="singleInstance" and its orientation is always portrait. Second activity is always has landscape orientation.
In some devices, everything is alright and in task manager there is only one instance of my app, but in some devices (like Samsung S7), when I launch SecondActivity, there will be two instances of my app in task manager like this image:
My guess is that something is wrong with the launchMode of the MainActivty but I need it to be singleInstance. Any suggestion?
EDIT:
MainActivity in manifest:
<activity
android:name=".Activities.MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"
android:theme="#style/AppTheme"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and second:
<activity
android:name=".Activities.SecondActivity"
android:screenOrientation="landscape" />
launching code:
Intent intent = new Intent(getActivity(),
intent.putExtra("VideoUri", filmGet.getOutput().getData().getFilmTrailer());
startActivity(intent);
If it helps, I launch SecondActivity from a fragment.
So, after reading #sharan comment and some googling, it made me to read some google documents. According to docs, there isn't any difference between android:launchMode=singleInstance and android:launchMode=singleTask but one. They both make your activity singleton and so you will never have two instances of it. The only difference between them is that singleInstance will prevent the task from attaching any other activity while singleTask has not this limitation. Any other things about them are the same.
So, for anyone who is reading this post, I'll recommend you to never use singleInstance launch mode unless you exactly need what it has. Because if you have only one activity in your app, then there will be no differences between singleInstance and singleTask. And if you have more than one activity, then I'll recommend you to all of your activities belong to one task.
In short, change singleInstance to singleTask and you are go.
I want to clear the back stack within an Activity but not by startActivity() and using FLAG. e.g. when I launch an application from the application icon then the application main activity starts but there is some thing in the back stack like the launcher activity because when we touch the minimized app tab the launcher is visible . I want to remove the launcher application from the minimized applications.
#SorryForMyEnglish's answer is right. You just cannot to implement it. By using android:noHistory="boolean" attribute, see my concept maps below:
Because of ActivityC and ActivityD (last activities) has a true value, so they cannot back to MainActivity, but they can back to ActivityA and ActivityB. Also, ActivityA and ActivityB can back to MainActivity. And the backstack is completely cleared without using this startActivity(intent) to open your next Activity (so you will need FLAG):
Intent intent = new Intent(CurrentActivity.this, NextActivityToBeOpened.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
After you declared the value in manifest, you just need to call this startActivity(intent) to open the next Activity (no FLAG is needed):
startActivity(new Intent(CurrentActivity.this, NextActivityToBeOpened.class));
Is it very simple, right?
Remember:
Set your last Activity with android:noHistory="true" in manifest.
Another Activity must be set with false value.
Apply this attribute for all of your Activity.
As additional, here is how to use it inside your manifest:
<activity android:name=".MyActivity" android:noHistory="true" />
use android:noHistory="true" attribute in Manifest for your Activity
<activity android:name="StartActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
I have an activity registered on APPWIDGET_CONFIGURE:
<activity android:name="com.tahanot.activities.NearbyStops">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
However this activity does not behave as expected. It opens inside of an existing stack, and when I press the Back button, it takes me to other activities instead of closing the task. Ideally, I would like the APPWIDGET_CONFIGURE intent to include FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_MULTIPLE_TASK.
Is it possible to specify flags in AndroidManifest.xml, and if not, what workaround would you suggest?
Consider specifying launchMode attribute to the activity element.
<activity android:launchMode="singleTask" android:name="com.tahanot.activities.NearbyStops">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
As per the official docs,
FLAG_ACTIVITY_NEW_TASK
Start the activity in a new task. If a task is already running for
the activity you are now starting, that task is brought to the
foreground with its last state restored and the activity receives the
new intent in onNewIntent().
This produces the same behavior as the "singleTask" launchMode value,
discussed in the previous section.
Since you mention you want FLAG_ACTIVITY_NEW_TASK behavior so singleTask launchMode might work for you.
USe this java code when you start your activity
Intent intent = new Intent(this,
activityname.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Here you can add flag with your intent object like this.
And you can also add multiple flag.
This is the manifest declaration that I used, following appsroxcom's idea for android:launchMode:
<activity
android:name="com.tahanot.activities.NearbyStops"
android:configChanges="orientation"
android:label="#string/title_activity_stops_nearby"
android:launchMode="singleTop"
android:taskAffinity="com.tahanot.tasks.widgetConfiguration" >
<!-- android:launchMode makes sure the Back button doesn't navigate to another NearbyStops activity.
android:taskAffinity makes sure the Back button doesn't navigate to some other activity. -->
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
I have a ListActivity and a MapActivity. I would like to launch either one of these activities on application startup that has been chosen by the user in a preferences window.
So far the only way I see to launch an activity on application startup is to specify it in the application manifest file using...
<activity android:name=".MyActiivty"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I am thinking I might have to start an activity that does nothing but looks at the user preferences and then launches either the ListActivity or MapActivity. Seems like a waste to have an activity do nothing but launch another activity. In my research I have not found any solution to this problem. Any suggestions would be greatly appreciated.
Thanks & Regards,
Dave
First, don't create some third activity. Just have the LAUNCHER Activity be either the list or the map, and have it call startActivity() on the other one (plus finish()) in onCreate() before calling setContentView() when needed. That way, ~50% of the time, you're launching the right activity.
In principle, you could have both activities have a LAUNCHER <intent-filter>, only enabling one. However, that will not work with desktop shortcuts, which will route to a specific activity (whichever one happened to be configured when they made the shortcut). If this does not concern you, you might go this route. However, try to test it with a few devices and custom home screens -- I'm not sure if everyone will pick up on your change immediately.
I just added the following code to the onCreate() method an it worked like a charm.
Intent intent;
intent = new Intent(this, MyMap.class);
startActivity( intent );
finish();
for new folks (me), following is dave's answer, plus changes i needed to make to AndroidManifest.xml.
Main activity:
Intent intent;
intent = new Intent(this, DisplayMessageActivity.class);
startActivity( intent );
changes to xml file, from -> http://developer.android.com/training/basics/firstapp/starting-activity.html
AndroidManifest.xml:
<activity
android:name="com.mycompany.myfirstapp.DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName="com.mycompany.myfirstapp.MyActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mycompany.myfirstapp.MyActivity" />
</activity>