Save Setting onDestroy - android

How to save setting after we exit the app using onDestroy?
Example:
When the app start, it will start Main_Activity.class
Button button1;
public class Main_Activity extends Activity {
super.onCreate(savedInstanceState);
................
}
Added a button named "button1" and give an Action to open new activity when clicked
public void button1_newactivity (View v){
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener (new View.OnClickListener() {
public void onClick(View arg0) {
Intent secondactivity=new Intent (getApplicationContext(), Second_Activity.class);
startActivity(secondactivity);
}
});
}
Added 2 Checkbox on Second_Activity.class, as a default when the app start checkbox1 is selected and checkbox2 is not selected. But, when checkbox2 selected and checbox1 automatically not selected, after pressed another button it will start Third_Activity.class.
My Question is how can we save this setting, so when we exit the app, then start the app again, It will automatically start Third_Activity.class not Main_Activity.class like the first one?
What should we write in this part
protected void onDestroy(){
....................
}

use SharedPreferences to store which will be your first activity. launch your launcher activity like before. But in there check the value you saved in sharedpreference. So if you find that you have to start 3rd activity from the oncreate of launcher start the third and finish the first one. For example
public class Main_Activity extends Activity {
super.onCreate(savedInstanceState);
SharedPreferences pref = getSharedPreferences(name);
boolean b = pref.getBoolean("should_start_third", false);
if(b){
finish();
start third activity
}
................
}
Here in SharedPreferences i used a should_start_third boolean value to check if the third activity will start directly. this is by default false.
you have to save the value of shared preferences after the third checkbox is selected. to save use like following.
getSharedPreferences(name).edit().putBoolean("should_start_third", true).commit();

Related

can I change launcher activity in MainAvtivity

How can I change Launcher activity in android studio using Button click in Java file??
for example, if user clicks Button1 then a activity will be launcher activity and if user clicks Button2 then another activity will be a launcher activity???
Which activity is launched at startup is determined by the AndroidManifest.xml file, you cannot modify this at runtime.
However, you can make an activity whose sole purpose is to launch the app and then choose which other activity to launch:
in button 1 click, save activity 1's name into a sharedpreferences entry
in button 2 click, save the other's name
in the launch activity, check the value of the sharedpreferences entry and launch an intent with the correct activity based on that
Edit: example for what you are trying to do
In your Button1 click:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get the sharedpreferences with the name "myPreferencesName"
// and the mode "MODE_PRIVATE" (i.e. only you can read the settings stored in it)
// and then get its editor
SharedPreferences.Editor editor = getApplicationContext().
getSharedPreferences("myPreferencesName", Context.MODE_PRIVATE).
edit();
editor.putString("activityToLaunch", "activity1"); // activity1 can be whatever string you choose, not neccessarily the actual name of the activity
editor.apply();
}
});
In your button2 click, you do the same but put "activity2" as the preference value
In your MainActivity onCreate:
SharedPreferences prefs = getApplicationContext().
getSharedPreferences("myPreferencesName", Context.MODE_PRIVATE);
if(!prefs.contains("activityToLaunch")) {
// neither button has been pressed since you installed the app
// this always happens on the first launch after installation
// do the editor thing here as you would in the button click
// depending on which activity you want to be default, either set the preference value to "activity1", or "activity2", then editor.apply() it
}
if(prefs.getString("activityToLaunch").equals("activity1")) {
Intent intent = new Intent(getApplicationContext(), Activity1.class);
startActivity(intent);
}
else {
Intent intent = new Intent(getApplicationContext(), Activity2.class);
startActivity(intent);
}

Dynamically Add button from one activity to another

i am working on an app in which, button Onclick dynamically created a button from one activity and button is appeared in another activity.
So, for your button, see the code below. I'm just using SharedPreferences for testing if button has been clicked before or not.
// Inside your onCreate() method of your SecondActivity.java
((Button)findViewById(R.id.activity2_button)).setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).edit().putBoolean("ShowButton", true).commit(); // put Boolean inside SharedPreferences
Intent main = new Intent(this, FirstActivity.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(main);
finish();
}
}
And now, the FirstActivity.java code :
public class FirstActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1_layout);
if (getSharedPreferences("SharedPreferences", Context.MODE_PRIVATE).getBoolean("ShowButton", false))
((Button)findViewById(R.id.activity1_button)).setVisibility(View.VISIBLE);
else
((Button)findViewById(R.id.activity1_button)).setVisibility(View.GONE);
}
}
Test it, and tell me if this works great. Hope it will work for you, Darkball60 :)

onRestoreInstance not getting called while switching between activities

I m trying to switch between two activities .
In activity 1, i have Button named GoToSecond to goto second activity.
In the same way,in activity 2, have Button named GoToFirst to goto first activity.
I have used log messages in first activity .
Order I m getting when GoToSecond button is clicked is
onCreate
onStart
onResume
onSaveInstance
onStop
and it switches to second activity .
Now in second activity when i click button GoToFirst , first activity opens and
log order in first activity is
onCreate
onStart
onResume ..
why onRestoreInstance is not getting called after onStart?
even if instance is stored??
Can anyne help me?
Here is the code of first activity:
public class MainActivity extends AppCompatActivity {
EditText hello;
Button b1;
public static String TAG="Prajwal";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hello=(EditText) findViewById(R.id.hello);
Log.d(TAG,"OnCreate counter 1");
b1=(Button) findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
I havent added log messages in code .
And second activity code is
public class Main2Activity extends AppCompatActivity {
Button b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b2=(Button) findViewById(R.id.b2);
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(Main2Activity.this,MainActivity.class);
startActivity(i);
}
});
Log.d(MainActivity.TAG,"Oncreate 2");
}
For example
If i hv an EditText in first activity , and if i hav some text in it , I m losing that text while switching between 2nd activity to 1st activity . bcoz onCreate is called in first activity.
But when i switch to landscape mode onRestoreInstance is called and i m not losing any text .
As I know, onRestoreInstance will get called only if your activity will be destroyed and created again. So in this case your will restore previous state of destroyed activity.
But in your case activity isn't destroyed, it just stopped by the system, so it is not loose its state, so no need to restore it.
Please see these links for more details: FIRST, SECOND
Edited
Since you provided code of your Activities, as I see, you open from second activity not first activity, but third activity with type of first Activity.
So your stacktrace after pressing on button in second Activity would be:
MainActivity -> Main2Activity -> MainActivity.
So you have two simple solutions:
1) Instead of
Intent i=new Intent(Main2Activity.this,MainActivity.class);
startActivity(i);
Just write finish();
2) Add flag FLAG_ACTIVITY_CLEAR_TOP to intent from Main2Activity to MainActivity

jump from main activity to last visited activity

Lets suppose I am at 4th activity of my app and I have ten activities. I close the activity from 4th level now when i reopen my activity again
i have a button on main activity when i click on it I must have to go on 4th activity where i left last time. Now what I do is that I saved every activity number or ID in Shared preference like that
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level1);
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putInt("level", 4);
editor.apply();
Then i retrieve it in main activity like this:
btnconti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE);
int restoredLevel = prefs.getInt("level", 0);
if (restoredLevel >0) {
}
}
});
Now can any body tell me how i can jump to my last visited activity?
If you have seperate Activities for all 10 activities u can call an intent as follows inside multiple if conditions of restoredlevel
Intent i = new Intent(getApplicationContext(),ActivtyOfyourlevel.class)
startActivity(i);

how to start from different Activity in android studio

i make app with two activities.
firs has:
2 ExitText(login and password);
button(confirm, save data with SharedPreferences, intent to second activity).
second one:
2 TextView(get login and password with SharedPreferences);
button(clear data on SharedPreferences, intent to firsActivity).
how to make next: while there are some data on SharedPreferences - app will be started from the 2nd screen.
for example, i made:
if (user!=null && pass!=null){ Intent enterIntent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(enterIntent);
}
but, technically it first run the firstActivity and than go to the secondOne. if there are some method to start app with the another activity (not mainOne)?
You won't be able to check to see if there are values in SharedPreferences before entering one Activity.
What you can do is check the value before displaying the UI (before calling setContentView(R.layout.my_layout)), and either continue along, or start the next Activity.
public class MyStartActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if (preferences.contains("my_key")) {
// start next Activity
}
setContentView(R.layout.my_layout);
}
}
If you don't want the first one on the back stack, you can call finish() after starting the second one (or use appropriate flag(s) on intent).
Another approach would be to have only one activity with to fragments and decide dynamically which one set on start. With fragments you can also easily change layouts on button click or on back pressed.
I'm not sure if it will work, because app has to start at the first activity. First activity checks login and pass at share preferences and then you can go to the second activity
try this
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preference = getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
if (preference.getSting("key",null)!= null) {
// start new Activity
//finish this activity so it not in back stack
} else {
setContentView(R.layout.my_layout);
}
}

Categories

Resources