CharSequence Restarts After Orientation Change - java

I have a CharSequence which displays a sequence of text after each imageview click however the CharSequence seems to restart if the orientation is changed mid sequence.
Does anyone know how this can be resolved?

On an orientation change the activity is restarted, and the inCreate() is called again. You have to take that in consideration.

A small example of how to store and retrieve a value:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
isStarted = savedInstanceState.getBoolean("isStarted");
}
}
#Override
protected void onResume() {
isStarted = true;
super.onResume();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean("isStarted", isStarted);
super.onSaveInstanceState(outState);
}
For more information and methods: Saving Android Activity state using Save Instance State

using android:configChanges="orientation|keyboardHidden|screenSize"> resolved the issue

Related

Saving the value of toggle buttons when activity gets destroyed and put them back when activity starts again

enter image description hereGood day, I want to save the value of the toggle buttons when the app exits and put them back when the user starts the app again. I've tried using onSaveInstanceState but it just doesn't work. Any help would be appreciated. Thank you.
edit: I also tried using preferences.
edit: I also tried using preferences.
edit: I also tried using preferences.
edit: I also tried using preferences.
edit: I also tried using preferences.
edit: I also tried using preferences.
#Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putBoolean("white", white.isChecked());
savedInstanceState.putBoolean("yellow", yellow.isChecked());
savedInstanceState.putBoolean("orange", orange.isChecked());
savedInstanceState.putBoolean("red", red.isChecked());
savedInstanceState.putBoolean("blue", blue.isChecked());
savedInstanceState.putInt("whiteI", whiteI.getVisibility());
savedInstanceState.putInt("yellowI", yellowI.getVisibility());
savedInstanceState.putInt("orangeI", orangeI.getVisibility());
savedInstanceState.putInt("redI", redI.getVisibility());
savedInstanceState.putInt("blueI", blueI.getVisibility());
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
white.setChecked(savedInstanceState.getBoolean("white"));
yellow.setChecked(savedInstanceState.getBoolean("yellow"));
orange.setChecked(savedInstanceState.getBoolean("orange"));
red.setChecked(savedInstanceState.getBoolean("red"));
blue.setChecked(savedInstanceState.getBoolean("blue"));
whiteI.setVisibility(savedInstanceState.getInt("whiteI"));
yellowI.setVisibility(savedInstanceState.getInt("yellowI"));
orangeI.setVisibility(savedInstanceState.getInt("orangeI"));
redI.setVisibility(savedInstanceState.getInt("redI"));
blueI.setVisibility(savedInstanceState.getInt("blueI"));
}
#Override
protected void onPause() {
super.onPause();
SharedPreferences.Editor editPreferences = preferences.edit();
editPreferences.putBoolean("white", white.isChecked());
editPreferences.putBoolean("yellow", yellow.isChecked());
editPreferences.putBoolean("orange", orange.isChecked());
editPreferences.putBoolean("red", red.isChecked());
editPreferences.putBoolean("blue", blue.isChecked());
editPreferences.putInt("whiteI", whiteI.getVisibility());
editPreferences.putInt("yellowI", yellowI.getVisibility());
editPreferences.putInt("orangeI", orangeI.getVisibility());
editPreferences.putInt("redI", redI.getVisibility());
editPreferences.putInt("blueI", blueI.getVisibility());
editPreferences.commit();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) Toast.makeText(this, "null", Toast.LENGTH_SHORT).show();
setContentView(R.layout.activity_main);
background = findViewById(R.id.imageViewBg);
yellow = findViewById(R.id.tgBtnAlarm);
orange = findViewById(R.id.tgBtnAlert);
red = findViewById(R.id.tgBtnCritical);
blue = findViewById(R.id.tgBtnRecession);
white = findViewById(R.id.tgBtnNormal);
yellowI = findViewById(R.id.imageViewYellow);
redI = findViewById(R.id.imageViewRed);
blueI = findViewById(R.id.imageViewBlue);
orangeI = findViewById(R.id.imageViewOrange);
whiteI = findViewById(R.id.imageViewWhite);
btnExit = findViewById(R.id.btnExit);
preferences = getSharedPreferences("pref",MODE_PRIVATE);
white.setChecked(preferences.getBoolean("white"));
yellow.setChecked(preferences.getBoolean("yellow"));
orange.setChecked(preferences.getBoolean("orange"));
red.setChecked(preferences.getBoolean("red"));
blue.setChecked(preferences.getBoolean("blue"));
whiteI.setVisibility(preferences.getInt("whiteI"));
yellowI.setVisibility(preferences.getInt("yellowI"));
orangeI.setVisibility(preferences.getInt("orangeI"));
redI.setVisibility(preferences.getInt("redI"));
blueI.setVisibility(preferences.getInt("blueI"));
glide();
allow();
volume();
btnExit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
Try using SharedPreferences
SharedPreferences savePreferences = getSharedPreferences("pref",MODE_PRIVATE);
SharedPreferences.Editor editPreferences = savePreferences.edit();
editPreferences.putBoolean("white", white.isChecked());
editPreferences.putBoolean("yellow", yellow.isChecked());
editPreferences.putBoolean("orange", orange.isChecked());
editPreferences.putBoolean("red", red.isChecked());
editPreferences.putBoolean("blue", blue.isChecked());
editPreferences.putInt("whiteI", whiteI.getVisibility());
editPreferences.putInt("yellowI", yellowI.getVisibility());
editPreferences.putInt("orangeI", orangeI.getVisibility());
editPreferences.putInt("redI", redI.getVisibility());
editPreferences.putInt("blueI", blueI.getVisibility());
editPreferences.commit();
To retrive data
white.setChecked(savePreferences.getBoolean("white"));
yellow.setChecked(savePreferences.getBoolean("yellow"));
orange.setChecked(savePreferences.getBoolean("orange"));
red.setChecked(savePreferences.getBoolean("red"));
blue.setChecked(savePreferences.getBoolean("blue"));
whiteI.setVisibility(savePreferences.getInt("whiteI"));
yellowI.setVisibility(savePreferences.getInt("yellowI"));
orangeI.setVisibility(savePreferences.getInt("orangeI"));
redI.setVisibility(savePreferences.getInt("redI"));
blueI.setVisibility(savePreferences.getInt("blueI"));

How to implement onPause() and onResume?

When I'm leaving my app, my counter gets reset.
I know that I need to use the onPause and onResume functions. However, I don't know how to write it in the code.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button b1 = findViewById(R.id.b1);
eggcounter = 100;
final ImageButton ImgButton = findViewById(R.id.eggBtn);
ImgButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
eggcounter = eggcounter - 1;
updateEgg();
if (eggcounter < 80) {
ImgButton.setImageResource(R.drawable.egg_2);
if (eggcounter <60){
ImgButton.setImageResource(R.drawable.egg_3);
if (eggcounter <40) {
ImgButton.setImageResource(R.drawable.egg_4);
If you want to save your application state, you can use Shared Preferences. Example :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// You can call to onResume() or on Pause() in onCreate();
onResume();
onPause();
}
#Override
public void onResume() {
super.onResume();
// After receiving this call you will usually receive a following call
// to onStop() and return to your activity 's onResume()
}
#Override
public void onPause() {
super.onPause();
// When you receive a call, your activity is going into the background
// (to onPause) but your activity can be killed to reclaim resources
// (if there are not enough resources to start the new activity)
}
so if you want to save your instance application state, you can save to Shared Preferences, SQLite and so on. Call it in onPause() or onStop() and reclaim this data in onResume()

How to save a Button drawable when rotating screen

I just read this: Saving Android Activity state using Save Instance State
this: Android - Open resource from #drawable String
My situation is: I have a Button background set when pressing it to be green with
button.setBackgroundResource(R.drawable.greenbutton);
How can I store this information with onSaveInstanceState and onRestoreInstanceState?
I tried: How to maintain a button style and click state after screen rotation? and worked but maybe there is something better than a three-nested-if-conditionals procedure? I mean I have to do this for 4+ Button and I think it a lot of work for just a simple cause :)
Thank you
EDIT: this is the code so far
package com.example.android.testbutton;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.change);
}
Button button;
Boolean click;
public void changeColor(View view) {
click = true;
button.setBackgroundResource(R.drawable.greenbutton);
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("buttonClicked", click);
// etc.
super.onSaveInstanceState(savedInstanceState);
}
//onRestoreInstanceState
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
Boolean firstAnswer = savedInstanceState.getBoolean("buttonClicked");
{
if (savedInstanceState != null) {
if (savedInstanceState.containsKey("buttonClicked")) {
if (savedInstanceState.getBoolean("buttonClicked"))
button.setBackgroundResource(R.drawable.greenbutton);
//some codes to make the button becomes clicked.
}
}
}
}
}
set a variable on your activity too, and then save that and restore it with your state.
public class MyActivity extends Activity {
public static final String KEY_BUTTON_IS_GREEN = "isGreen";
boolean buttonIsGreen;
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(KEY_BUTTON_IS_GREEN, buttonIsGreen);
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
buttonIsGreen = savedInstanceState.getBoolean(KEY_BUTTON_IS_GREEN, false);
if (buttonIsGreen){
// find the button and set it green.
}
}
you'll need to add to your method that sets the button green and also set your variable = true if the button is green.
You have a few options:
You can set specific flags in AndroidManifest file:
<activity name= ".YourActivity" android:configChanges="orientation|screenSize"/>
It does not work by default because , when you change the orientation onCreate will be called again and it redraws your view. But If you set these flags and you are using a different layout for landscape mode, by adding these parameters the layout for landscape mode will not be called, because onCreate will not be called second time.
You can save int resources by:
int colorFirst = answerOne.getHighlightColor();
savedInstanceState.putInt("key", colorFirst);

Chronometer restarts on change orientation

I reset the chronometer value when you change the orientation, has written is true, but does not help, ask for help, what went wrong
#Override
protected void onSaveInstanceState(Bundle outState) {
timer = SystemClock.elapsedRealtime() - chronometer.getBase();
outState.putLong("timerValue", timer);
super.onSaveInstanceState(outState);
}
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.details_layout);
chronometer = (Chronometer) findViewById(R.id.chronometer);
chronometer.setBase(SystemClock.elapsedRealtime());
if (savedInstanceState != null) {
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
public void onChronometerTick(Chronometer chronometer) {
timer = savedInstanceState.getLong("timerValue");
chronometer.setText(DateFormat.format("mm:ss", timer));
}
});
chronometer.start();
} else {
chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
public void onChronometerTick(Chronometer chronometer) {
timer = SystemClock.elapsedRealtime() - chronometer.getBase();
chronometer.setText(DateFormat.format("mm:ss", timer));
}
});
chronometer.start();
}
}
Caution: Your activity will be destroyed and recreated each time the user rotates the screen. When the screen changes orientation, the system destroys and recreates the foreground activity because the screen configuration has changed and your activity might need to load alternative resources (such as the layout).
See:
How to contain value of android chronometer with change in orientation

Android onsaveinstancestate()

so i get the main idea of how to use
protected void onSaveInstanceState (Bundle outState)
http://developer.android.com/reference/android/app/Activity.html#onSaveInstanceState(android.os.Bundle)
also from Saving Android Activity state using Save Instance State
but my problem is what if this was the first time the application is being created? then nothing would have been stored in the bundle before....and if so then when i try to call something out from the bundle that hasn't been saved before what do i get?null?
for example
i have this in my code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String [] b=savedInstanceState.getStringArray("MyArray");
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
String [] a={"haha"};
savedInstanceState.putStringArray("MyArray", a);
}
in the FIRST time the application is ever opened what would the value of b be?
and after the application has been used once what would the value of b be?
Many thanks!
in your onCreate()
add a condition
if(savedInstanceState==null){
//meaning no data has been saved yet or this is your first time to run the activity. Most likely you initialize data here.
}else{
String [] b=savedInstanceState.getStringArray("MyArray");
}
by the way to retrieve data that was saved in your onSaveInstanceState you will override this
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onRestoreInstanceState(savedInstanceState);
}
You have to check for null always in the onCreate() or in onRestoreInstanceState() like below:
String [] b = new String[arraysize];
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null)
{
b = savedInstanceState.getStringArray("MyArray");
// Do here for resetting your values which means state before the changes occured.
}
else{
default..
}
Here you do general things.
}

Categories