2

I am developing an application. my requirement is that first time when i installing the application in to device it has to start the main and launcher activity. After that when i am starting/opening my application in side the device it has to load another activity instead of main and launcher. If the application is uninstalls and installs again it has to load the main and launcher again. can you please anybody share the solution on this kind of topics.

Thanks in advance.

Ganesh
  • 924
  • 2
  • 12
  • 34

5 Answers5

4

You can do this:

Say Activity A is the activity that you want to launch only the first time, and activity B the activity that the system will launch after the first time.

In you manifest put Activity B as your launcher activity. Then inside the oncreate or better OnResume of activity B put the following:

    @Override
protected void onResume() {
    super.onResume();

       if(firstLaunch()){
           startActivity(new Intent(this, A.class));
           finish();
       }else{
         //Do your normal stuff
       }

    }


    private boolean firstLaunch(){
         SharedPreferences prefs = getSharedPreferences(
            "Preferences",
            Context.MODE_PRIVATE);
         return prefs.getBoolean("firstLaunch",false);
    }

Then on your A activity be sure to set a flag on your preferences to indicate that your application has run more than once. So somewhere inside activity A put this:

   private void setFirsLaunchFlag(){
         SharedPreferences prefs = getSharedPreferences(
            "Preferences",
            Context.MODE_PRIVATE);
         SharedPreferences.Editor edit = prefs.edit();
         edit.putBoolean("firstLaunch",true);
         edit.commit();
   }
Julian Suarez
  • 4,499
  • 4
  • 24
  • 40
2

You can use a bool value like this:

1. When application is being launched for the first time make it true.

2. Check this bool value in your launcher activity and if it is true start your desired activity and if it false then make it true and save (for the first time).

Note: You can use SharedPreference for the bool value.

Vineet Shukla
  • 23,865
  • 10
  • 55
  • 63
0

I think this may help you:

If the App is first time launched android.intent.action.PACKAGE_FIRST_LAUNCH broadcast will be fired.

After receiving this broadcast maintain a flag in shared preference that is the app is launched first time.

Have a splash screen activity as your launcher activity and according to that flag redirect it to your one time activity or replacing activity.

Sadeshkumar Periyasamy
  • 4,848
  • 1
  • 26
  • 31
0

As far as ı know you don't have a chance to launch another activity. Instead you could keep the track of your installation inside SharedPreferences and run different code based on that.

guydemossyrock
  • 1,118
  • 9
  • 16
0

simply create an integer or Boolean shared preference variable.when ever the application starts at first instance display the activity which you want to be displayed just 1'c, at the same time change the value of this shared preference variable.every time from not when the app is just started you have to check this value and and if it is not the default value load another activity in main and launcher.

Here's a reference LINK , not exactly the expected ans..but hope it helps

Community
  • 1
  • 1
Sanjay D
  • 265
  • 4
  • 17