Home > database >  Android Studio - change launcher activity after certain instruction
Android Studio - change launcher activity after certain instruction

Time:10-27

I wanna change starting activity after the user will make certain instructions. Let's say that for now MainActivity.java starts every time when the user runs the app. I wanna change that to MainActivity2.java after the user will click on specified button in app. I'm trying to do that by using shared preferences but can't figure it out how to make it. I know that I need a context to start an activity, so do I need to create empty activity and then just put code with "if" instruction to run proper activity?

I already tried to do that on a simple java class (non-activity class) but it can't be done since it does not extend "AppCompatActivity".

CodePudding user response:

You can keep your existant launcher Activity, in the onCreate methode, check for your condition, change the Activity :

@Override
public void onCreate(Bundle b){
super.onCreate();
if(your_condition){
startActivity(new Intent(this, MainActivity2.class);
finish();
}
setContentView(R.layout_main_activity1);
}
  • Related