specifying a different home activity at run-time - android

To specify my "Home" activity at compile-time, I can use the following code in my AndroidManifest.
<activity
android:name=".HomeActivity"
<intent-filter android:label="#string/home_activity">
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I have a requirement though where I need to be able to specify the activity I would like to use as my "Home" screen at "run-time". Does anyone know if this is possible? Basically, I want to replace "HomeActivity" with something else.
I looked into using an "activity-alias" where I can specify the target activity using the "targetActivity" attribute but I didn't quite get how I can use this.
Thanks!

#Jon you can conditionally call separate activity from splash.Like you have a condition that on first app launch you need to open a tutorial screen and then onwards your home Activity then you can create different intents.
if (!sharedPreferences.contains(DiceConstants.FIRST_TIME_PREFS)) {
intent = new Intent(this, TutorialActivity.class);
sharedPreferences.edit().putBoolean(DiceConstants.FIRST_TIME_PREFS, true).commit();
} else {
intent = new Intent(this, HomeActivity.class);
}

Related

Android Lollipop App Notification Settings

In Android Lollipop, when you long press a Notification, it gives you access to settings for that Notification's app, such as priority, or simply blocking it. Is there an intent that I can use to access those settings?
Found the answer here - https://plus.google.com/+CyrilMottier/posts/YY6tbJrJMra
You need to add this to the activity you want to be attached to that settings icon
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.NOTIFICATION_PREFERENCES" />
</intent-filter>
In addition to the correct answer from #shmuel :
In case you want this "App settings" system button to jump to one specific fragment of your app's Settings activity deriving from PreferenceActivity, you can create a dummy intermediary activity to handle the intent-filter as details above, and then in this activity you simply do:
public class DeepSettingsFragmentDummyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent settingsIntent = new Intent(this, SettingsActivity.class);
// Standard mechanism for a PreferenceActivity intent to indicate
// which fragment to activate automatically.
settingsIntent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT,
YourTargetPreferenceFragment.class.getName());
startActivity(settingsIntent);
finish();
}
}
This code automatically launches the YourTargetPreferenceFragment fragment, and then dismisses itself (finish()) so as to not remain in the activity stack when the user hits Back.
Your Manifest must contain:
<activity
android:name=".DeepSettingsFragmentDummyActivity"
android:label="...">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.NOTIFICATION_PREFERENCES" />
</intent-filter>
</activity>

<activity> without <intent-filter> in AndroidManifest.xml

I know I need to use to enable an activity to receive an Intent like this (not main activity).
<activity android:name=".MyApp_2ndActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
However, I found it can be triggered too if "intent-filter" is removed, like this.
<activity android:name=".MyApp_2ndActivity">
</activity>
I am wondering what difference is in these 2 formats?
See here: http://developer.android.com/guide/components/intents-filters.html
The difference is the second one can only be started using an explicit Intent -- one which names the component it wants to start. The first one can be started by an implicit Intent -- one which does not specify the exact component but contains information for the system to find an appropriate match for. The intent filters are used by the system for resolving such intents.
Difference is the when we use this code:
<activity android:name=".MyApp_2ndActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
This will be the first activity triggered when you starts your application.It doesnot need any explicit intents
and when we use this code:
<activity android:name=".MyApp_2ndActivity">
</activity>
the activity will be started with the use of Explicit intent
Intent filters are used, for example, when the activity starts from a specific event on the device. Your main activity has specific intent filters. If you want your application start when NFC tag is scanned, you can specify that via intent filters.
You can read more here for example.
http://developer.android.com/guide/components/intents-filters.html

Purpose of using CATEGORY_HOME in android manifest?

What is the alternative to removing the following from AndroidManifest:
<activity
android:name="com.apper.main.UserActivity"
android:label="#string/app_name"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
android:stateNotNeeded="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
I also found that after removing the above line, there was no impact to my android
application. What's the main use of this category and what's the alternative to it.
If the intent of this category is to launch the home screen then it can be done by the following:
Intent homeIntent= new Intent(Intent.ACTION_MAIN);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(homeIntent);
This above code will launch the home screen but why the line in the android manifest ?
What's the purpose as removing the line from here does nothing change the application ?
What's the main use of this category
It, coupled with ACTION_MAIN, identifies a replacement home screen.
what's the alternative to it
Not having it. Either you have this category, or you do not.
The category HOME is used to declare your application as a Home launcher. By putting this in the manifest then user will have the option to have your application open upon pressing the home button.
This is typically used when creating an application that will be used in a kiosk mode.
I do not believe there is an alternative to make an application a home launcher.
Documentation

How to export an activity so other apps can call it?

Well I searched a lot, but I didn't find a precise answer how to export an Activity, so an app can start it with startActivityforResult.
How do I achieve that? Do I have to change the Manifest in some ways?
As an alternate to Dalmas' answer, you can actually export an Activity without creating an <intent-filter> (along with the hassle of coming up with a custom action).
In the Manifest edit your Activity tag like so:
<activity
android:name=".SomeActivity"
....
android:exported="true" />
The important part is android:exported="true", this export tag determines "whether or not the activity can be launched by components of other applications". If your <activity> contains an <intent-filter> then this tag is set to true automatically, if it does not then it is set to false by default.
Then to launch the Activity do this:
Intent i = new Intent();
i.setComponent(new ComponentName("package name", "fully-qualified name of activity"));
startActivity(i);
Of course with this method you will need to know the exact name of the Activity you are trying to launch.
You need to declare an intent-filter in your Manifest (I took the following example from Barcode Scanner) :
<activity android:name="...">
<intent-filter>
<action android:name="com.google.zxing.client.android.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Then create an intent with the same action string :
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, code);
Android should start your activity (or it will show a drop-down box if there are multiple apps sharing the same action string).

How to start an activity

I have created two classes: Example1 and Example2, which extends activity.
Example1 contains a UI of buttons, while Example2 contains UI of TextEdit. I want to call Example2 when a button is clicked, so this code is in an onclick method
Intent i = new Intent();
i.setClassName("com.a.ui", "com.a.ui.Example2");
startActivity(i);
So I am able to get the UI of Example2 successfully. What's an alternate way of calling intent?
Is there any alternate way to start an activity?
you can call like this:
startActivity(new Intent(com.a.ui.this, com.a.ui.Example2.class));
You can try this way.
Intent i = new Intent(com.a.ui.this, com.a.ui.Example2.class);
startActivity(i);
And remember to include the Activity in your Manifest.xml
e.g.
<activity android:name=".SampleActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Categories

Resources