MainActivity, p1, p2, p3의 4 가지 활동이 있습니다.앱을 다시 실행하면 Android 공유 환경 설정이 작동하지 않습니다.
내 앱이 정상적으로 작동하지만 앱이 강제 종료되거나 집 버튼으로 앱을 닫으면 앱을 다시 열 때 공유 성능이 지워지고 다시 시작 버튼이 앱에서 종료됩니다.
MainActivity :
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_page);
final Button resume = (Button) findViewById(R.id.resume);
Button next = (Button) findViewById(R.id.next);
Button exit = (Button) findViewById(R.id.exit);
resume.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String PREFS_NAME = "MyPrefsFile";
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getBoolean("my_first_time", true)) {
resume.setEnabled(false);
Log.d("Comments", "First time");
settings.edit().putBoolean("my_first_time", false).commit();
}else
{
MainActivity.this.finish();
}
}
});
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, p1.class);
startActivity(intent);
}
});
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}
}
XML을 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="resume"
android:layout_width="wrap_content"
android:id="@+id/resume"
android:layout_height="wrap_content" />
<Button
android:text="next"
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"/>
</LinearLayout>
P1 :
public class p1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.p1);
Button next = (Button) findViewById(R.id.next);
Button home=(Button)findViewById(R.id.home);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(p1.this, p2.class);
startActivity(intent);
}
});
home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(p1.this, MainActivity.class);
startActivity(intent);
}
});
}
private void storeCurrentActivity(){
SharedPreferences myPref =getSharedPreferences("APP_SHARED_PREF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=myPref.edit();
editor.putString("lastactivity", p1.this.getClass().getSimpleName());
editor.commit();
}
@Override
public void onResume(){
super.onResume();
storeCurrentActivity();
}
}
XML :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/next"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="page 1"/>
<Button
android:text="go in main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home"/>
</LinearLayout>
과 P2, P1과 같은 P3.
먼저 한 OnCreate이에 추가 해결하는'MyPrefsFile'와'APP_SHARED_PREFS'을. 그것 이외에 :'lastactivity'의 값을 설정하고 있습니다 만, 결코 되돌릴 수는 없습니다. – Bobby
만약 내가 도와 주길 원한다면 정확한 코드를 넣거나 편집해야하는 정확한 위치를 말해줘. 메신저 안드로이드와 나는 매일 이것을 수정하기 위해 1 주일 동안 시도합니다. 내 문제를 해결할 수 있다면 저를 도와주세요. – erfan