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();
}
}
}
我尝试了 ::
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();
}
}
}
日志 ::
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.
我的理解 ::
MainActivity
有关如何实现此目标的任何想法
答案 0 :(得分:1)
你永远不应该明确地呼叫onCreate()
。 Android在创建活动时调用它。您应该查看the activity lifecycle,看看当用户从设置活动中返回时,您是否无法将某些代码移动到另一个回调方法(例如onResume()
)。
如果您真的希望再次调用onCreate()
,则应该查看API级别11中添加的Activity.recreate()
。如果您低于该API,则可以使用以下代码解决此问题:
Intent intent = getIntent();
finish();
startActivity(intent);
这完成了活动,并以与首先创建它相同的意图再次启动它。