简体   繁体   中英

onDestroy() Called when specific Activity is Called

I have problem with onDestroy method() containing sharedPreferences Method Deletion. This method makes my welcome screen only appear once per app opening. I call it only when exit button pressed and Its working well. But, after I made a new activity called Daftar_Isi or Table Of Contents (In English) that contains list View, it works perfectly to go to targeted activity but it also calls the onDestroy method, it causes my main.xml / main activity showing welcome screen again although I didnt hit the exit button. I think the list on list view activity calls the onDestroy method.

I want to set the activity can open the targeted activity without calling onDestroy too.

This is my main 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();
}
}

And this, daftar_isi activity that contains List View

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

Remove finish() after startActivity(new Intent(Main.this, Daftar_isi.class));

finish() does not call onDestroy(). finish() usually triggers a call to onDestroy() .

Generally speaking, finish() will eventually result in onDestroy() being called.

But, according to docs onDestroy() can also be called if system is running low on memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM