Question

MainActivity.java

public class MainActivity extends FragmentActivity {

    LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            showDialog();

        }

    }

    public void showDialog(){
        //GPS-Dialog
        GpsEnablingDialog gpsAlert;
        try {
            gpsAlert = new GpsEnablingDialog();
            gpsAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("My-Log-Msg",e.toString());
            e.printStackTrace();
        }       
    }
}

I tried with::

import android.content.Context;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class MainActivity extends FragmentActivity {

    LocationManager locationManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        if ( !locationManager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
            showDialog();
            try {
                onCreate(savedInstanceState);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Log.d("My-Log-Msg",e.toString());
            }
        }

    }

    public void showDialog(){
        //GPS-Dialog
        GpsEnablingDialog gpsAlert;
        try {
            gpsAlert = new GpsEnablingDialog();
            gpsAlert.show(getSupportFragmentManager(), "GpsAlert_Tag");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Log.d("My-Log-Msg",e.toString());
            e.printStackTrace();
        }

    }
}

Log::

04-23 14:22:19.969: D/My-Log-Msg(1586): java.lang.IllegalStateException: Already attached
04-23 14:22:20.419: D/gralloc_goldfish(1586): Emulator without GPU emulation detected.

What i understand::

  • From the log i can clearly understand that i am trying to attach something that's already attached
  • Why i am doing this:: First time the activity is loaded, it checks for the GPS is enabled or not and a dialog pops up and takes me to Settings page
  • Now i enable the GPS, and press back button on the phone i return to MainActivity
  • i want to restart the activity lifecycles from begining and check these GPS and proceed further

Any ides on how to achieve this

Was it helpful?

Solution

You should never explicitly call onCreate(). Android calls it when an Activity is created. You should check out the activity lifecycle and see if you can't move some code to another callback method (such as onResume()) when the user returns from the settings activity.

If you really wish to have onCreate() called again, you should check out Activity.recreate() which was added in API level 11. If you're below that API you can work around this with the following code:

Intent intent = getIntent();
finish();
startActivity(intent);

This finished the activity, and starts it again with the same intent that it was created with in the first place.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top