Paldan
Paldan

Reputation: 435

Android onCreate() not working

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);

    }

Upvotes: 0

Views: 2784

Answers (5)

boaz levinson
boaz levinson

Reputation: 95

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">
...

Upvotes: 0

Brianjs
Brianjs

Reputation: 2339

Use the on resume function below.

@Override
public void onResume(){
     //will be executed onResume
}

Upvotes: -1

Jonik
Jonik

Reputation: 81820

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");
   // ....
}

Upvotes: 0

EJK
EJK

Reputation: 12534

"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

Upvotes: 2

James andresakis
James andresakis

Reputation: 5415

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.

Upvotes: 0

Related Questions