hossam scott
hossam scott

Reputation: 466

Start different launcher-activty from MyApplication

My Code In AndroidManifest.xml the launcher is set for X activity,

inside public class MyApplication extends Application I have:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
}

What I'm trying to do: Inside MyApplication to check if the user is running the app from phone or tablet with the method you can see at the top and go to activity B if it's a tablet, or activity A if it's a phone.

What I have done so far:

inside onCreate()

if (!isTablet(MyApplication.this)) {
    Intent intent = new Intent(MyApplication.this, SplashScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
} else {
    Intent intent = new Intent(MyApplication.this, XlSplashScreen.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
}

Is there a way to omit the launcher Activity in the manifest and let the Application class decide which activity should start?

Upvotes: 1

Views: 107

Answers (4)

Sam
Sam

Reputation: 5392

You should be allowing Android to natively handle this for you through folder structure.

res/layout-sw600dp/ # For 7” tablets (600dp wide and bigger)

res/layout-sw720dp/ # For 10” tablets (720dp wide and bigger)

res/layout-sw600dp-port/ # For 7” tablets in portrait (600dp wide or bigger)

res/layout-sw720dp-port/ # For 10” tablets in portrait (720dp wide or bigger)

res/layout-sw600dp-land/ # For 7” tablets in portrait (600dp wide or bigger)

res/layout-sw720dp-land/ # For 10” tablets in portrait (720dp wide or bigger)

So if you are designing for Portrait with a few landscape variants, then you can make a -land folder.

If you are designing for Landscape with a few portrait variants, then you can make a -port folder.

Or you can simply reply on the size folders and don't create any -land or -port folders if the design works fine for both.

Upvotes: 1

Marco Fincato
Marco Fincato

Reputation: 163

You could create a new Activity with no layout, and set it as the launch activity. Then, in the onCreate() method of this activity, check if it's a tablet or a phone and start the desired Activity.

However, I think you should avoid this approach, and give a look to Fragments.

UPDATE

You can't make the Application class decide which Activity should be started. The Application class contains common parts of your app that should work even if your app is not being shown. In fact it is often used to set event listeners for notifications, since it happens that for this class to be instantiated even if the app is not on screen.

An alternative solution could be making two different apps, one for phones and the other for tablets. However the Fragments approach is still the best.

Upvotes: 1

Hanna
Hanna

Reputation: 71

Since your activity is a splash screen, I assume the main difference between two of them is UI configuration and sizes. So the best thing you can do is to run the same activity but have different layout files or resources for different screen resolutions and orientation. This is the right way to work with tablets.

Upvotes: 1

ILLIA DEREVIANKO
ILLIA DEREVIANKO

Reputation: 790

You can display different splashscreens using the drawable(-s) folders.

More: https://developer.android.com/guide/topics/resources/providing-resources

The application class not intended to call startActivity(intent) and etc.

Upvotes: 0

Related Questions