Can I launch different Activities on startup depending on user preferences? - android

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>

Related

Android handling click on notification

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.

Two activities in task manager of same app

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.

How to start another Activity when you have the Activity name String but not the Activity.Class?

I'm running into a problem starting other activities from mine. I know this must be being done elsewhere as there are so many launcher apps out there which much use package manager to start specific activities...
I can get an Acitivity name I would like to start from the package manager, but how can I somehow parse this and turn it into an intent? Baring in mind I can't access the class... Also I would like to start that specific activity and not launch the MAIN intent from the package...
I'm sure someone must be doing this somewhere... It's kind of the point in activities isn't it?
Assuming you have following in your AndroidManifest.xml
<!-- The askUser dialog activity -->
<activity android:theme="#android:style/Theme.Dialog"
android:name="my.app.AskUserActivity"
android:excludeFromRecents="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="my.app.intents.AskUserConfirmConnect"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then you can call this Activity by name, like this:
Intent dlgIntent = null;
dlgIntent = new Intent("my.app.intents.AskUserConfirmConnect");
dlgIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
dlgIntent.addFlags(Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
context.startActivity(dlgIntent);
You can use setClassName(String, String) on the Intent to avoid needing the other class.

android check if an activity was started from an action or from another activity?

In my manifest file, I have an activity declaration which looks something like this:
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This activity is obviously launched on application start. User can navigate from this activity to another activity and from that one to another etc.
In another activity, I start MainActivity by using 'startActivity' method, ie. something like:
Intent intent = new Intent(AnotherActivity.this, MainActivity.class);
startActivity(intent);
In my MainActivity (in onCreate() method maybe), can I determine whether an activity was started from a action of from another activity? Is there something like "launcher listener"? I would like to avoid putting any extra content in the intent.
Can I simply put String s = getIntent().getAction(); in onCreate method and check whether it is has a value of MAIN?
Well the stock android launcher does send Intent.ACTION_MAIN as as the action. However, you can't be sure that some other launcher will have the same behaviour. Your best bet will be to pass some extra data with the Intent.

Problem while displaying activity from BroadcastReceiver

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>

Categories

Resources