OnPause() and also onDestroy() never called when other activity opened
我有一个使用intent的许多活动的应用程序。我有一个用于保存值的sharedPreferences方法。
我有一个欢迎屏幕,我设置在应用程序启动时显示(调用onCreate)。在同一个活动中,我删除了sharePreferences方法,因此当我重新打开应用程序时,欢迎屏幕将再次启动。
问题是,当我更改为另一个活动并再次返回主活动并按退出按钮时。当我再次启动应用程序时,它不会显示欢迎屏幕。我认为,主要活动仍然在运行,因此,onPause(),onStop()和onDestroy()永远不会被调用。当我保持相同的活动(主)并按退出时,它将调用onDestroy()(包含erase sharedPreferences方法)。
我没有设置finish()方法。如果我设置了它,则每个移动的活动都将在Main活动中调用onDestroy,因此欢迎屏幕将在每次移动到该活动时启动。
我的mainactivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | package com.bani.latihan; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.EditText; import android.widget.RelativeLayout; public class Main extends AppCompatActivity { RelativeLayout PopupScreen, layoutAsli; @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } @Override public void onDestroy(){ super.onDestroy(); SharedPreferences settings = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE); settings.edit().remove("ditekan").apply(); } @Override public void onCreate(Bundle savedInstanceState) { this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setLogo(R.mipmap.ic_launcher); getSupportActionBar().setDisplayUseLogoEnabled(true); setContentView(R.layout.main); Button btn1 =(Button)findViewById(R.id.button1); final Button btn2 =(Button)findViewById(R.id.button2); PopupScreen = (RelativeLayout)findViewById(R.id.PopupScreen); layoutAsli = (RelativeLayout)findViewById(R.id.layoutAsli); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Intent intent2 = new Intent(Main.this, Intent2.class); startActivity(intent2); } }); btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub moveTaskToBack(true); finish(); } }); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view,"Isi dewek cuk!", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); EditText tvText = (EditText) findViewById(R.id.editText1); SharedPreferences prefs = getSharedPreferences("Preferences", Context.MODE_PRIVATE); if (prefs.contains("text")){ tvText .setText(prefs.getString("text","")); } SharedPreferences prefs2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE); if (prefs2.contains("ditekan")){ PopupScreen.setVisibility(View.INVISIBLE); layoutAsli.setVisibility(View.VISIBLE); } } public void dismisWelcomeMessageBox(View view) { PopupScreen.setVisibility(View.INVISIBLE); layoutAsli.setVisibility(View.VISIBLE); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public void onPause() { super.onPause(); EditText tvText = (EditText) findViewById(R.id.editText1); Button btWelcome = (Button) findViewById(R.id.button_welcome); SharedPreferences.Editor prefEditor = getSharedPreferences("Preferences", Context.MODE_PRIVATE).edit(); SharedPreferences.Editor prefEditor2 = getSharedPreferences("PreferencesWelcome", Context.MODE_PRIVATE).edit(); prefEditor.putString("text", tvText.getText().toString()); prefEditor2.putBoolean("ditekan", btWelcome.isPressed()); prefEditor.apply(); prefEditor2.apply(); } } |
我的另一个活动代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | package com.bani.latihan; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.view.WindowManager; import android.widget.Button; import android.widget.CompoundButton; import android.widget.ImageView; /** * Created by Bani Burhanuddin on 21/02/2016. */ public class Intent2 extends AppCompatActivity { @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } @Override protected void onCreate(Bundle savedInstanceState) { this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.onCreate(savedInstanceState); getSupportActionBar().setDisplayShowHomeEnabled(true); getSupportActionBar().setLogo(R.mipmap.ic_launcher); getSupportActionBar().setDisplayUseLogoEnabled(true); setContentView(R.layout.intent); Button btnnext = (Button) findViewById(R.id.button5); Button btnhome = (Button) findViewById(R.id.button6); Button btnback = (Button) findViewById(R.id.button7); btnnext.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(Intent2.this, Intent3.class)); finish(); } }); btnhome.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(Intent2.this, Main.class)); finish(); } }); btnback.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub startActivity(new Intent(Intent2.this, Main.class)); finish(); } }); CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1); final ImageView imageView2 = (ImageView) findViewById(R.id.imageView2); imageView2.setVisibility(View.GONE); toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { imageView2.setVisibility(View.VISIBLE); } else { imageView2.setVisibility(View.GONE); } } }); SharedPreferences prefs = getSharedPreferences("Preferences2", Context.MODE_PRIVATE); if (prefs.contains("text2")) { toggle.setChecked(prefs.getBoolean("text2", true)); } } @Override public void onPause() { super.onPause(); CompoundButton toggle = (CompoundButton) findViewById(R.id.switch1); if (toggle.isChecked()) { SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit(); editor.putBoolean("text2", true); editor.apply(); } else { SharedPreferences.Editor editor = getSharedPreferences("Preferences2", MODE_PRIVATE).edit(); editor.putBoolean("text2", false); editor.apply(); } } } |
我的清单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <!-- Splash screen --> <activity android:name="com.bani.latihan.splashscreen" android:label="@string/app_name" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation|screenSize" android:noHistory="true"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- Main activity --> <activity android:name="com.bani.latihan.Main" android:screenOrientation="sensor" android:label="@string/app_name" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <!-- Intent2 activity --> <activity android:name=".Intent2" android:screenOrientation="sensor" android:noHistory="true" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <!-- Intent3 activity --> <activity android:name=".Intent3" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> <!-- Intent4 activity --> <activity android:name=".Intent4" android:screenOrientation="sensor" android:configChanges="keyboardHidden|orientation|screenSize"> </activity> </application> </manifest> |
我的主要布局
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | <RelativeLayout android:id="@+id/PopupScreen" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#123456" android:orientation="vertical" android:layout_margin="16dp" android:padding="16dp" android:visibility="visible"> <TextView android:id="@+id/welcome_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="16dp" android:layout_marginTop="16dp" android:text="@string/welcome" android:textColor="#ddd333" android:textSize="28sp" android:textStyle="bold" /> <TextView android:id="@+id/welcome_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/welcome_title" android:text="@string/welcome_message" android:textColor="#0dff00" android:textSize="18sp" /> <Button android:layout_width="wrap_content" android:id="@+id/button_welcome" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_gravity="bottom" android:background="#3b978d" android:onClick="dismisWelcomeMessageBox" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="@string/ok" android:textColor="#fff" /> </RelativeLayout> <RelativeLayout android:id="@+id/layoutAsli" android:visibility="invisible" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" android:id="@+id/hello_world"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1" android:layout_below="@+id/hello_world"/> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/imageView" android:src="@drawable/imageview" android:layout_gravity="center" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next" android:id="@+id/button1" android:layout_marginTop="111dp" android:layout_centerHorizontal="true" android:layout_gravity="left|bottom" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Exit" android:id="@+id/button2" android:layout_below="@+id/button1" android:layout_centerHorizontal="true" android:layout_marginTop="59dp" android:layout_gravity="right|bottom" /> </FrameLayout> </RelativeLayout> </RelativeLayout> |
我有你的问题。 目前你正在使用MoveTaskToBack。 它不会破坏活动,它只是在你完成之后回到活动堆栈中
活动。 所以它没有调用其他回调方法。 它总是调用onDestroy
使用moveTasktoBack或完成。 不要同时使用它们,这会令人困惑。