If the user has already set my app as HomeScreen, is there an easy way to switch the Home Activity?

StackOverflow https://stackoverflow.com/questions/23660714

  •  22-07-2023
  •  | 
  •  

Question

I am making an app, but there are basically two different, albeit similar Home Screens. There is the one Home Screen designated for personal use, and the other one distinctly for business use. Both Home Screens are launchers from which you can launch the apps that correspond to that Home Screen.

Now I set my Home Screen this way:

ComponentName component = new ComponentName(HomeScreen1.class.getPackage().getName(), HomeScreen1.class.getName());
packageManager.setComponentEnabledSetting(component, packageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);

When I launched a personal app and then press Home, I want it to go to the personal apps Home Screen, when I am on the business Home Screen (it's just a different activity you can get to by pushing a button on the first Home Screen), if I launch a business app, I want pressing Home to bring me to this second Home Screen.

I realize that one way to do this is to just have one Home Screen activity and kind of change the setContentView() arguments so that it visually changes, but this becomes very unmanageable. Since the user has nominated my app as a Home Screen Replacement App, even though technically only one Activity is nominated as the Home Screen, I was wondering if there was a way to, when the user presses the Home button go through some control statement and launch one of the two Home Screens?

It need not necessarily be that simple a solution, but basically I'm trying to avoid constantly overriding the setContentView() method, if possible.

EDIT: In case it's unclear, I have HomeScreenPersonal and HomeScreenBusiness, currently pressing Home button always brings you to HomeScreenPersonal. I was wondering if there was a way to easily have 2 home screens basically where the Home Screen is dependent on some high level boolean.

Was it helpful?

Solution 2

I figured out an interesting and functional way to get the behavior I want. Basically create only one HomeScreen with:

public static boolean personalMain = true;

private Activity activity = this;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (personalMain) {
        new HomeScreenPersonal(activity);
    } else {
        new HomeScreenBusiness(activity);
    }
}

Then instead of having those two be Activity objects, just make them regular classes whose constructors specify the setContentView(). Fairly clean way of having multiple Home Screens.

OTHER TIPS

You can use SharedPreference to store any app wide variables (http://developer.android.com/reference/android/content/SharedPreferences.html). Here is an example on how to use SharedPreferences (http://developer.android.com/training/basics/data-storage/shared-preferences.html).

If you want to give your user more options (ie changing the home screen), you may want to create a Settings activity as recommended by this guide http://developer.android.com/guide/topics/ui/settings.html.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top