У меня проблема с методом onDestroy(), содержащим метод удаления разделяемых разделов. Этот метод делает мой экран приветствия только один раз за открытие приложения. Я называю это только при нажатии кнопки выхода и его работе. Но после того, как я сделал новую деятельность под названием Daftar_Isi или Table Of Contents (на английском языке), которая содержит список View, он отлично работает, чтобы перейти к целевому действию, но также вызывает метод onDestroy, он вызывает мой main.xml/main activity, показывающий снова приветствуем экран, хотя я не нажал кнопку выхода. Я думаю, что список активности в режиме просмотра списка вызывает метод onDestroy.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;
}
}
}
О чем вы используете приложение? – Exception
@exeption Конечно, на моем телефоне Android работает lollipop –
remove finish() метод, когда вы вызываете направленную активность – justDroid