How to change starting activity in android studio?

Viewed 112

Hello I am developing a simple project with android studio and I wanted to know how can I change the starting activity of the application via Java Code. I know how to do it with the androidmanifest.xml but I want the user to insert some data in the starting activity and then the next time the user loads the application the main activity pops up directly and not the starting activity again.

Thank you.

2 Answers

Use shared preference, with this you can save limited information. depending on what your user has selected you can save it and start an activity from it. the information remains even after the application is closed.

e.g. so you create a start activity that checks what settings have been selected and depending on that it starts the required activity with an intent.

search for shared preference and intent.

You should save a flag in Shared preference. For example in a first activity that launch save this:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
 editor.putString("name", "isNew");
 editor.apply();

And when you want to Retrieve data from preference:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String name = prefs.getString("name", "default value");//"default value defined" is the default value. 

Then Check if (name == isNew) then start your MainActivity class

Related