2016-09-20 1 views
0

Я разрабатываю приложение, в котором я поддерживаю сеанс, используя SharedPreference. Чтобы пользователь нажал кнопку «Выход», он отключится только от приложения. Но проблема в том, что пользователь в своем телефоне закрывает последние приложения, в нашем приложении снова отображается экран входа в систему. Я не знаю, как это сделать. Пожалуйста, помогите. Ниже мой код mainActivity:Поддержание сеанса в android с использованием общей помехи

SharedPreferences sharedpreferences; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity1); 

    submit = (Button) findViewById(R.id.btn_login); 

    ImageView i1 = (ImageView) findViewById(R.id.imgLogo); 
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.pb); 
    String checkBoxText = "I agree to all the "; 
    final CheckBox checkBox = (CheckBox) findViewById(R.id.checkBox); 
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
    checkBox.setText(Html.fromHtml(checkBoxText)); 
    checkBox.setMovementMethod(LinkMovementMethod.getInstance()); 

    submit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      final EditText e1 = (EditText) findViewById(R.id.input_email); 
      final EditText p1 = (EditText) findViewById(R.id.input_password); 
      final String e = e1.getText().toString(); 
      final String password = p1.getText().toString(); 

      SharedPreferences.Editor editor = sharedpreferences.edit(); 

      editor.putString(email, e); 

      editor.commit(); 

и код для второй деятельности:

 else if(position==3) 
      { 
       AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); 
       builder.setMessage("Are you sure you want to Logout?") 
         .setCancelable(false) 
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 

           SharedPreferences sharedpreferences = getSharedPreferences(main.MyPREFERENCES, Context.MODE_PRIVATE); 
           SharedPreferences.Editor editor = sharedpreferences.edit(); 
           editor.clear(); 
           editor.commit(); 
           Intent i = new Intent(getApplicationContext(), main.class); 
           // Closing all the Activities 
           i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

           // Add new Flag to start new Activity 
           i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

           // Staring Login Activity 
           getApplicationContext().startActivity(i); 


           finish(); 
          } 
         }) 
         .setNegativeButton("No", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int id) { 
           dialog.cancel(); 
          } 
         }); 
       AlertDialog alert = builder.create(); 
       alert.show(); 

      } 
+0

Где вы проверить, что вы уже вошли в систему? –

ответ

0

Вот простое решение для этого.

пользователь никогда не сможет закрыть приложение из последних, используя это. Ему нужно закрыть его из своей деятельности. Убедитесь, что если работа

<activity 
android:name="youActivity" 
android:theme="@android:style/Theme.NoDisplay" 
android:autoRemoveFromRecents="true"/> 

android:autoRemoveFromRecents="true" Может помочь вам

+0

Сделайте это в файле манифеста. –

+0

Я сделал в своей основной деятельности, который является экраном входа, но он не работает –

+0

Применить его к второму действию не первый –

Смежные вопросы