0

I'm having a strange problem to do with stopping my android app. On my phone I have a home button and a back button, now when I go into my app after pressing the home button, the program loads data from the internet as expected, but when I go into my app after pressing the back button, the data doesn't load. I've debugged it to an extent, and have found out that the only difference is that the back button calls the onCreate() method. I'm quite confused to why this is is happening.

Here's some of my code:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.d("DAP", "Created");

        setContentView(R.layout.activity_ltc);

        getActionBar().setTitle("LTC Charts");
        getActionBar().setLogo(
                getResources().getDrawable(R.drawable.new_litecoin_logo_large));

        TextView textView = (TextView) findViewById(R.id.ltcdata);
        textView.setText("Loading data...");

        TimerTask timer = new TimerTask() {

            @Override
            public void run() {

                parseJSON();
            }

        };

        Timer time = new Timer();
        time.schedule(timer, 500, 85);

    }
Jonik
  • 80,077
  • 70
  • 264
  • 372
Paldan
  • 395
  • 1
  • 6
  • 16

5 Answers5

2

"when I go into my app after pressing the back button, the data doesn't load."

If you have already launched your app, the Activity will be paused (and onPause is called) when you navigate away from it. When you navigate back to the app, the same activity instance is resumed (and onResume is called).

See http://developer.android.com/training/basics/activity-lifecycle/index.html

EJK
  • 12,332
  • 3
  • 38
  • 55
  • 1
    On pressing the back button, activity gets killed. Following Methods of the activity gets called. onPause() => onStop() => onDestroy() While you re-launch the app, Following activity life cycle methods gets called. onCreate() => onStart() => onResume() Referred to the below link. http://stackoverflow.com/questions/5019686/what-methods-are-invoked-in-the-activity-lifecycle-in-the-following-cases So why will oncreate method not get called ? Please clarify – Prem Nov 27 '13 at 20:21
0

This is because your activity hasnt been destroyed. What you should do is put something into the onresume of your activity to get the data again when you come back to the activity. If you want the data to be destroyed for sure and the user never leaves the activity you can destroy everything in onpause.

James andresakis
  • 5,335
  • 9
  • 53
  • 88
  • onDestroyed is called when I press the back button but not the home button. This confuses me because when I start the app again after pressing the back button onCreate() is called and the program doesn't work as expected. – Paldan Nov 27 '13 at 20:04
  • Im pretty sure thats because the back button pops the activity off the stack and finishes the activity while the home button just pauses everything and escapes the app. Go some other apps and check their behavior. Some of them reopen to where you were and others will manually destroy everything in onpause. – James andresakis Nov 27 '13 at 20:07
0

As others said, override onResume().

A common pattern would be to extract common initialisation code into a method, and call it from where needed:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    init();
}

@Override
protected void onResume(){
    super.onResume();
    init();
}

private void init(){       
   setContentView(R.layout.activity_ltc);
   getActionBar().setTitle("LTC Charts");
   // ....
}
Jonik
  • 80,077
  • 70
  • 264
  • 372
0

This could be because your activity is not set correctly at AndroidManifest.xml .

Make sure the activity name is the right one. If you inserted your activity into some package include that name as well . for example I have my activitu which is called "SettingsActivity" (usually the default is MainActivity) set in a package called Activities :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<activity android:name=".Activities.SettingsActivity">
...
-1

Use the on resume function below.

@Override
public void onResume(){
     //will be executed onResume
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Brianjs
  • 2,079
  • 2
  • 20
  • 32