onSavedInstanceState doesn't save my Instance - java

Hope you're all doing fine in these tough times.
I have 2 activities, A and B.
A has a button that enables an intent to go from A to B. B itself has a Dialog window whom's cancel button enables an Intent to go from B to A.
I'd like to save some data from A (2 EditTexts' content) so when I come back to A I don't have to retype those EditTexts.
So after looking up the documentation and a bit of StackOverflow, I decided to go with the onSavedInstance method but I'm wondering whether the intent transition destroys the Activity and hence the savedInstance as well...
Here's my simplified code - Activity A :
public class A extends AppCompatActivity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_match);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (savedInstanceState != null) {
EditText firstPlayer = findViewById(R.id.firstPlayerName);
EditText secondPlayer = findViewById(R.id.secondPlayerName);
String firstPlayerName = savedInstanceState.getString("firstPlayerName");
String secondPlayerName = savedInstanceState.getString("secondPlayerName");
firstPlayer.setText(firstPlayerName);
secondPlayer.setText(secondPlayerName);
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
EditText firstPlayer = findViewById(R.id.firstPlayerName);
EditText secondPlayer = findViewById(R.id.secondPlayerName);
String firstPlayerName = String.valueOf(firstPlayer.getText());
String secondPlayerName = String.valueOf(secondPlayer.getText());
savedInstanceState.putString("firstPlayerName", firstPlayerName);
savedInstanceState.putString("secondPlayerName", secondPlayerName);
}
}
When I debugged it, I did notice it passed thru onSavedInstance and it did save the values, but once in onCreate, the savedInstanceState is null.
Any advice?
Thanks in advance & take care people.
Fares.

You note that the "cancel button enables an Intent to go from B to A.". This sounds like you're creating a new instance of Activity A (so your activity stack goes A, B, A), and the instance state is specific to the individual instance.
Instead, you should return to A by simply calling finish() on B. This will return to the original instance of A, which will have your data.

Related

set textview from antoher activity in android

I'm having a problem with my app.
So, I'm trying to save some data into another activity, from mainactivity inputs. And I think I found the error but can't fix it.
I believe my app is searching for textview reference in wrong activity, in main. The problem is that I want to change another activity's textview. Is there anybody that could explain for me how to do that or give me some advice here?
public void saveEvent(View saveView){
Saved save=new Saved();
save.SaveNow(day,tvResultBefore.getText().toString(), tvResultAfter.getText().toString());
// Save class
public class Saved extends MainActivity {
TextView tvMonthResult;
TextView tvResultBef;
TextView tvResultAft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
tvMonthResult=findViewById(R.id.tvMonthResult);
tvResultBef=findViewById(R.id.tvResultBef);
tvResultAft=findViewById(R.id.tvResultAft);
}
public void SaveNow(String day, String resBef, String resAft){
tvMonthResult.setText(month);
tvResultBef.setText(resBef);
tvResultAft.setText(resAft);
}
get values from MainActivity like this ::
MainActivity.class
String data = editText.getText().toString();
Intent intent=new Intent(context,AnotherActivity.class);
intent.putExtra("DATA",data);
startActivity(intent);
Now, in AnotherActivty get value and set it to textview :
AnotherActivity.class
if(getIntent!=null)
String data = getIntent().getStringExtra("DATA");
if(data!=null)
textView.setText(data);
it solved your problem.

Using SharedPreference to save an activity in android?

I have an app where in the mainActivity the user has about 5 options to choose from. Clicking one of them opens a new activity but essentially all 5 opens up identical activities with different headings. In the newly opened activities, users use multiple rating bars to delegate points to some specified attributes. Using SharedPreference, can I save the entire activity so when I back out, click on the same option everything isn't gone? Or do I need to save let's say, the individual rating bar values using the SharedPreference?
Here is some code for one of the activities that opens from a button click. Something is terribly wrong because it is crashing now. Any suggestions?
public class MageSkillScreen extends AppCompatActivity
{
public float skillPoints = 10;
public float strengthRating;
public float intellectRating;
public float wisdomRating;
public float dexterityRating;
public float totalSkill;
public float mageStrength;
public float mageDexterity;
public float mageIntellect;
public float mageWisdom;
public RatingBar strengthBar;
public RatingBar intellectBar;
public RatingBar wisdomBar;
public RatingBar dexterityBar;
Button submit;
//preferences
SharedPreferences magePref;
boolean rememberRatings = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mage_skill_screen);
strengthBar = (RatingBar) findViewById(R.id.mageStregth);
intellectBar = (RatingBar) findViewById(R.id.mageInt);
wisdomBar = (RatingBar) findViewById(R.id.mageWisdom);
dexterityBar = (RatingBar) findViewById(R.id.mageDext);
submit = (Button) findViewById(R.id.submit);
}
#Override
public void onPause()
{
SharedPreferences.Editor edit = magePref.edit();
edit.putFloat("strengthPts", strengthBar.getRating());
edit.putFloat("dexterityPts", dexterityBar.getRating());
edit.putFloat("intellectPts", intellectBar.getRating());
edit.putFloat("wisdomPts", wisdomBar.getRating());
//edit.putString("pointsLeft", ptsRemaing.getText().toString());
edit.commit();
super.onPause();
}
#Override
public void onResume()
{
super.onResume();
rememberRatings = magePref.getBoolean("mageRatings", true);
mageStrength = magePref.getFloat("strengthPts", 0.0f);
mageDexterity = magePref.getFloat("dexterityPts", 0.0f);
mageIntellect = magePref.getFloat("intellectPts", 0.0f);
mageWisdom = magePref.getFloat("wisdmPts", 0.0f);
}
}
update view activity in function onResume
#Override
protected void onResume() {
super.onResume();
//load SharedPreferences again and update view
}
Okay, let's go step by step.
So I have an app where in the mainActivity the user has about 5
options to choose from.
Okay, sounds good, so you have 5 buttons in MainActivity for users.
Clicking one of them opens a new activity but essentially all 5 opens
up identical activities with different headings.
Can, you please be more specific here, click on one button should launch one single activity not all 5. And this is not possible at a time only one activity exists for the user to view or interact with. Others, goes in the backstack.
Please read about activity lifecycle for the same.
https://developer.android.com/guide/components/activities/activity-lifecycle.html
Using SharedPreference, can I save the entire activity so when I back
out, click on the same option everything isn't gone?
No, you can't save the whole activity inside Sharedpreference. SharedPref is used to store key value pairs, like hashMap and only primitive values, you can store activities and you should never do it.
https://developer.android.com/reference/android/content/SharedPreferences.html
Now, coming back to the solution for the same.
It totally depends on the usecase you are trying to implement, if you are launching different activities on each button click and want to persist some data across other activities, store the primitive values in sharedpref
and then access it in other activities.This also holds, true if you want to persist the same data in app re-launch.
If not, then you can have a singleton object and modify it and access it, across other activities, make sure to make it null to avoid memory leak.
I hope it clears your doubt.
Cheers..!!

How to populate the EditText data while on resuming the state?

Here is the following scenario:
Filling the EditText and other Fields in Activity A
Moving from Activity A to Activity B on Buttonclick
Now Moving from Activity B to Activity A on Button Click
populated EditText is gone now. They are all empty
These fields should be pre-populated (as user has already filled them)
Is there any way to save the current state of Activity A. So that I can populated the EditText back? If not then How can I achieve the above task?
Use SharedPreferences.Editor to save the EditText data onPause, and retrieve the data with SharedPreferences onResume. That's the easiest way.
How are you moving from b back to a? If youre using an intent then your starting a new instance of that 'a' activity. Like some one mentioned above you should be using finish in your b activity which would take you back to a. If you need to populate a with data from b then you should use startActivityForResult - http://developer.android.com/training/basics/intents/result.html
To save the activity state, override OnSaveInstanceState() method.
#Override
protected void onSaveInstanceState(Bundle savedInstanceState)
{
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString("key", somevalue); //save your data in key-value pair
}
and restore the value using,
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState)
{
super.onRestoreInstanceState(savedInstanceState);
somevalue = savedInstanceState.getString("key");
}
Use this method if you are destroying the activity and creating it again.

How can I let another class know that a button was clicked (Android Studio)

Below is the code which I'm using to return a number which should be 1 when the button is clicked. However when I try to get that number from another class, it always stays 0.
As you might recognize, I tried to change the number in the onClickListener and returned it below.
I also tried to use the onPause command so that it will return the number onPause but it still doesn't work.
public class MainActivity extends Activity {
public int number;
Button btnAngled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btnAngled = (Button) findViewById(R.id.btnAngled);
final Intent intent = new Intent(this, angledForeheadActivity.class);
btnAngled.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
number = 1;
startActivity(intent);
}
});
}
#Override
protected void onPause() {
super.onPause();
}
public int getNumber() {
return number;
}
}
I try to get the code in another class with:
MainActivity a = new MainActivity();
int number = a.getNumber();
Sorry for the noob question..
declare the variable as static variable. Then you can simply obtain the result you want since there is only one copy of that variable. If you want to pass the value using intent, you can call putExtra() of intent to carry information to another activity.
Intent reference page
What you actually want is getting the number from another Class. Don't mix the job with button click together. You should setup the concept of model to store data and seperate UI and data, UI just change/get the data.
I suggest you either of the two ways
Store the number in some global model, then you can get the number from another Class.
User Android Broadcast to transfer the data
Use static variable in Activity is not a good idea, it may cause memroy leak, though it can solve your problem.

Android call method from another activity

How would I call a method from a different activity?
In my main activity, I have a button that shows a dialog to set the difficulty level of the game.
Then you click start game which starts a new activity containing a view with all the game information.
I need to send the difficulty level chosen to the other activity but cannot seem to figure out how to.
You could put it in the extras with the intent:
Intent StartGame = new Intent(this, StartGame.class);
StartGame.putExtra("difficulty", difficultyLevel);
startActivity(StartGame);
Then in your StartGame.class you can retrive it like this(assuming its a string):
Bundle extras = getIntent().getExtras();
if (extras != null) {
String difficulty= extras.getString("difficulty");
}
Well I don't know how sound my solution is but I created a myApplication class which sub classes the Application class .
This holds the reference to the activity I wanted to call
import android.app.Application;
public class myApplication extends Application {
public PostAndViewActivity pv;
}
When the PostAndViewActivity calls the oncreate is sets the pv to point to itself.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
((myApplication) getApplication()).pv = this;
Then when I want to call the method I want I just use code like this:
((myApplication) getApplication()).pv.refreshYourself();
Perhaps a bit hacky but it works.....
I welcome some critisism for this ;-)

Categories