2

I'm working on an app that has two activities. When I go from one activity to another then return I loss the data. Imagine: First activity shows a random number on a text view that changes on a button click Second activity does nothing it only has a return to the main activity button

*I know this app is worthless but its easier than showing you my large application...

So you have a random number on the textview, I go to the next activity then return to the main activity and the textview loses the number and shows another.

What the question is, is how to return to the activity without lossing the data (random number)? I dont want to save it on a database or on the sharedprefes since that wouldn't be good when being used in a large app with many data shown... Thx in advance

Edit:

Code to act1:

//public clas... oncreate...
Onclick(...){
      Intent intent = New intent (this,      acttwo.class);
      StartActivity (intent);
      Finish ();
}

Code to act2:

//public clas... oncreate...
Onclick(...){
     Intent intent = New intent(this,    actone.class);
     StartActivity (intent);
     Finish ();
}
user370305
  • 108,599
  • 23
  • 164
  • 151
Jpasker
  • 37
  • 1
  • 7
  • Sorry not to mention it's an android app written in Java – Jpasker Feb 04 '15 at 20:47
  • 1
    There is no reason that your TextView shouldn't show the number that was assigned to it before, unless you are assigning the random number within the onResume() or similar life-cycle component. If you are then you should do so only within onCreate so it happens only once at startup time. – Jay Snayder Feb 04 '15 at 20:48
  • What happens is that once I return the activity starts all over again – Jpasker Feb 04 '15 at 20:50
  • No need to mention that it's an android app written in Java, that's what the tags are for ;) And are you finishing the first activity before starting the second? – codeMagic Feb 04 '15 at 20:50
  • Simple, is your activity recreated ? Or You are updating your TextView in `onResume()` of your activity then it may be happen. – user370305 Feb 04 '15 at 20:51
  • When you start the app it shows 0, when you click it shows a random, when u switch activities it shows 0 – Jpasker Feb 04 '15 at 20:52
  • I didnt create an onResume – Jpasker Feb 04 '15 at 20:53
  • As Jay said: may be you assigned the value in `onResume()`; if not: did you rotate the device meanwhile? This might cause an `onCreate()` as well. In addition you should take care that you first activity is not closed by calling `finish()`. Finally, are you sure that your system isn't configured to aggressively clean up memory (check developer options) ... – Trinimon Feb 04 '15 at 20:54
  • 1
    show us to code to start activity2 and the code to come back to activity1 – ben75 Feb 04 '15 at 20:54
  • I edited the question sorry for wasting ur time – Jpasker Feb 04 '15 at 21:01
  • Could whoever down voted explain why? – Jpasker Feb 05 '15 at 15:52

4 Answers4

2

Your problem cause, because

You are calling finish() in Activity 1 when you starting Activity 2.

So remove finish() from Activity 1. and implement onActivityResult()

Like,

Onclick(...){
      Intent intent = New intent (this, acttwo.class);
      StartActivityForResult(intent, 0); // Result to ensure about back track of proper activity
}

Now Activity 2 just call finish() no need to call start Activity 1 again as Activity 1 is already in back stack of Application task.

So it should be,

//public clas... oncreate... of acttwo

Onclick(...){
     finish(); // We are just finishing Activity 2 not starting Activity 1 again 
}

Note:

In future it may happen Android system may kill your Application on Low Memory issue, so better to save your data and state of activity in SharedPreferences or Database.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I called finish since when I press on the back the previous activity shows then when I press it again the previous activity shows... – Jpasker Feb 04 '15 at 21:09
  • I would suggest you to go thru the android developer site http://developer.android.com/training/basics/activity-lifecycle/recreating.html which describe how to maintain activity state and android activity life cycle. – user370305 Feb 04 '15 at 21:13
0

You need to save your data one way or another. Without using DB or other classical methods(shared preferences) used for saving data in Android, as I see it, you are left with two options:

  1. Pass the data with Intents as extras
  2. Save it using method described in Accepted Answer at Saving Activity state in Android
Community
  • 1
  • 1
atastrumf
  • 183
  • 1
  • 7
0

You could use a static variable in your Activity to hold whatever random number your TextView is meant to hold and set its text to that every onResume() call. Something like;

class MyActivity extends Activity {
    private static int randomNumber;
    private TextView myTextView;

    ...

    @Override
    public void onResume(Bundle b){
        super.onResume(b);
        myTextView.setText(randomNumber);
    }
}
Fab Castel
  • 552
  • 5
  • 18
-1

Your TextView should persist the number that you had before. If you were setting these values in the onResume() method, then they would change every time the activity resumes. If you terminate your activity with a call to finish (while calling B) and then start a new activity A again (from B), then it will show a random number

If you have to do either of the methods I mentioned, you can add the values you want to persist in an intent as an extra, pass this to the second activity and then pass it back like so:

// Start activity B and pass the value you want to save in there
Intent A = new Intent(this, ActivityB.class);
A.putExtra("value", number);

// Start activity A from B and pass the value you saved in there
Intent B = new Intent(this, ActivityA.class);
B.putExtra("value", getIntent().getExtra("value");

Then retrieve number and set the TextView in your onResume (or onCreate if you are starting a fresh activity. But in this case do a check to see if the intent starting this activity has an extra named value - if yes, then display it else generate a new random number).

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • 1
    Well.. I explained this assuming a number since you had stated a number. Depending on your use, for streams of data (like photos, videos), you will add them as a serializable extra. If it is a regular object, then you will add them as a parcelable extra (or you can make that object implement serializable if it can). The principle is the same. I just used a primitive extra. – ucsunil Feb 04 '15 at 21:08
  • And your problem comes from calling finish() - I explained that bit – ucsunil Feb 04 '15 at 21:08