onDestroy() 在调用特定 Activity 时调用

onDestroy() Called when specific Activity is Called

我对包含 sharedPreferences 方法删除的 onDestroy 方法 () 有疑问。这种方法使我的欢迎屏幕在每次打开应用程序时只出现一次。我只有在按下退出按钮并且运行良好时才调用它。 但是,在我创建了一个名为 Daftar_Isi 或 Table 的新 activity 包含列表视图的内容(英文)之后,它可以完美地转到目标 activity 但它也调用 onDestroy 方法,它会导致我的 main.xml / main activity 再次显示欢迎屏幕,尽管我没有按下退出按钮。 我认为列表视图 activity 上的列表调用了 onDestroy 方法。

我想设置activity不用调用onDestroy也能打开目标activity

这是我的主要activity

    package com.bani.latihan;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;
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);
    Button btnMenu =(Button)findViewById(R.id.buttonMenu);
    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



            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                finishAffinity();
            }


        }
    });

    btnMenu.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // TODO Auto-generated method stub
            startActivity(new Intent(Main.this, Daftar_isi.class));
            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();
}
}

而这个 daftar_isi activity 包含列表视图

    package com.bani.latihan;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;


public class Daftar_isi extends ListActivity {

String[] halaman = new String[] {  "Halaman 2 : Image Viewer", "Halaman 3 : Text Viewer", "Halaman 4 : Check Box",
        "Halaman 5 : Radio Button", "Halaman 6 : Spinner", "Halaman 7 : Date Picker" };

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    super.onCreate(savedInstanceState);

    ArrayAdapter aa = new ArrayAdapter(this,
            android.R.layout.simple_list_item_1, halaman);
    setListAdapter(aa);

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);

    Toast.makeText(Daftar_isi.this, "Kamu Memilih  " + halaman[position],
            Toast.LENGTH_SHORT).show();

    switch (position){
        case 0:
            startActivity(new Intent(Daftar_isi.this, Intent2.class));
            break;

        case 1:
            startActivity(new Intent(Daftar_isi.this, Intent3.class));
            break;

        case 2:
            startActivity(new Intent(Daftar_isi.this, Intent4.class));

            break;

        case 3:
            startActivity(new Intent(Daftar_isi.this, Intent5.class));

            break;

        case 4:
            startActivity(new Intent(Daftar_isi.this, Intent6.class));

            break;

        case 5:
            startActivity(new Intent(Daftar_isi.this, Intent7.class));

            break;
    }
}
        }

删除 startActivity(new Intent(Main.this, Daftar_isi.class));

之后的 finish()

finish() 不调用 onDestroy()。finish() 通常会触发对 onDestroy().

的调用

一般来说,finish()最终会导致onDestroy()被调用。

但是,根据 docs 如果系统 运行 内存不足,也可以调用 onDestroy()