Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get instance of current android.intent.category.LAUNCHER activity

I have created a library project that I am sharing across several apps. I implemented a simple session expiration feature that will kick the user back to the login screen after a certain time period.

The login screen activity is my main activity, so in the manifest it looks like this:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock.Light.DarkActionBar"
    android:name="com.blah.application.MyApplication" >
    <activity
        android:name="com.blah.activity.LoginScreenActivity"
        android:label="@string/title_activity_main"
        android:screenOrientation="portrait"
        android:configChanges="orientation|keyboardHidden"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

When the session is expired, I want to kick the user back to the login screen, but I don't want to hardcode the name of the activity because it might be different depending on the specific app that is using the library. Here's what I was doing before:

Intent intent = new Intent(context, LoginScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(intent);

This doesn't work if the app's main activity is something different than LoginScreenActivity. I don't want to hardcode "LoginScreenActivity.class", I want to programmatically determine the name of the main class and then direct the user to that activity...can someone help me out?

Thanks in advance!

EDIT

I found a way to accomplish the same end result, but it's definitely not great. Since there is a certain amount of configuration necessary for me to deploy a new app using the same library (strings, bools, etc), I added a string to the strings.xml file for the specific app that defines the "main" activity name for that app:

<string name="mainClassName">com.blah.specificapp.activity.SpecificAppLoginScreenActivity</string>

Then I can get a handle on that class by name and redirect the user there:

Class<?> clazz = null;

try 
{
    clazz = Class.forName(context.getString(R.string.mainClassName));
} 
catch (ClassNotFoundException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

if(clazz != null)
{
    Intent intent = new Intent(context, clazz);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    context.startActivity(intent);
}

I know this is a god-awful solution, but it works. Like I said, there is a certain amount of configuration I have to do for each new app anyway, so adding one more string isn't a huge deal it's just not very elegant. I'd appreciate any suggestions that can accomplish the same goal without using my hack.

like image 527
DiscDev Avatar asked Jan 02 '13 21:01

DiscDev


People also ask

Can not find launcher activity in manifest?

It means you didn't specify an Activity for Android to launch as the default when the app opens from the launcher. You have to add an Intent Filter in the Manifest for the Activity you would like to act as the default when the app is being launched.

What is Intentfilter?

An intent filter declares the capabilities of its parent component — what an activity or service can do and what types of broadcasts a receiver can handle. It opens the component to receiving intents of the advertised type, while filtering out those that are not meaningful for the component.

What is android launcher activity?

Launcher Activities are the activities that can be launched for a given intent. For example, when you press an app icon on the home screen, the StartActivity intent starts the activity you have specified as the launcher activity.

What is category in intent android?

Categories are used for implicit Intents. So, If your Activity can be started by an implicit Intent when no other specific category is assigned to activity, activity's Intent filter should include this category. (even if you have other categories in the Intent filter).


1 Answers

You can request a launch Intent from the PackageManager, using:

Intent launchIntent = PackageManager.getLaunchIntentForPackage(context.getPackageName());

This will return an Intent that you can use to launch the "main" activity (which I assume is your "login" activity). Just add Intent.FLAG_ACTIVITY_CLEAR_TOP to this and you should be good to go.

like image 94
David Wasser Avatar answered Sep 28 '22 08:09

David Wasser