-3

Am trying to implementent onPause() and onResume(), such that when am out of the activity the the text in my text view is still there, but its displaying "null" when i start the the activity please assist!

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        etPassenger.setText(""+ gotPassenger );
        etStaffNumber.setText("" + gotStaffNumber);
    }



    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        etPassenger.setText(""+ gotPassenger );
        etStaffNumber.setText("" + gotStaffNumber);
    }
auselen
  • 27,577
  • 7
  • 73
  • 114
Weru
  • 1
  • 2
  • If you want to save your data even when closing your app you need to use `sharedPreferences`. Here's how you use them: http://stackoverflow.com/a/3624358/2649104 – Srujan Barai Aug 27 '15 at 16:47

4 Answers4

0

I think gotPassenger and gotStaffNumber vars are null - if you want to save them than you have to use prefs or save them to some bundle to restore later.

dilix
  • 3,761
  • 3
  • 31
  • 55
0

When you are minimizing your activity your variables becoming null. Add the following code in your activity,

@Override
public void onSaveInstanceState(Bundle savedInstanceState)
{
savedInstanceState.putString("gotPassenger",gotPassenger);
savedInstanceState.putString("gotStaffNumber",gotStaffNumber);
}

And, in your onCreate() method, add the following,

if(savedInstanceState!=null)
{
etPassenger.setText(savedInstanceState.getString("gotPassenger"));
etStaffNumber.setText(savedInstanceState.getString("gotStaffNumber"));
}
Nirmal Raghavan
  • 572
  • 7
  • 20
  • I hope you are closing the entire application. Then use `SharedPreferences` to store the values of gotPassenger and gotStaffNumber. Here's a tutorial for using `SharedPreferences` http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Nirmal Raghavan Feb 07 '13 at 08:07
0

When you are leaving your activity, you will loose the data stored inside gotPassenger and gotStaffNumber.
If you want to reuse them after exiting your application, you have to use SharedPreferences to save them first and retrieve them when you are back.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

If you want to save your data even when closing your app you need to use sharedPreferences. Here's how you use them

Community
  • 1
  • 1
Srujan Barai
  • 2,295
  • 4
  • 29
  • 52