onDestroy()在调用特定活动时调用

时间:2016-02-25 06:11:50

标签: android android-activity ondestroy

我对包含sharedPreferences方法删除的onDestroy method()有疑问。此方法使我的欢迎屏幕仅在每个应用程序打开时出现一次。我只在按下退出按钮并且工作正常时调用它。 但是,在我创建了一个名为Daftar_Isi或内容列表(英文)的新活动之后,它包含了列表视图,它完全适用于目标活动,但它也调用了onDestroy方法,它使我的main.xml / main活动显示为welcome屏幕再次虽然我没有按下退出按钮。 我认为列表视图活动上的列表调用onDestroy方法。

我想设置活动可以打开目标活动而不需要调用onDestroy。

这是我的主要活动

    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活动

    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;
    }
}
        }

1 个答案:

答案 0 :(得分:0)

finish()

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

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

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

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