使用“共享首选项”不会保存会话

时间:2018-04-20 17:00:49

标签: java android sharedpreferences

我有两项活动。在第一个活动中我有一个登录选项,在第二个活动中,我有两个按钮,即注销和关闭。 我的问题是,当用户登录并进入第二个活动时,当用户关闭应用程序而不退出并再次重新打开应用程序时,则需要显示第二个活动。

这是我的代码:

MainActivity.java

package com.example.user.store_session;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.session.MediaSessionManager;
import android.os.UserManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText name,password;
    Button login;
       public static final String ARB="ARBAZ";
       public static final String NAME="USERNAME";
    public static final String PASSWORD="PASSWORD";
       SharedPreferences sharedPreferences;


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

        name=(EditText)findViewById(R.id.Edit_text_name);
        password=(EditText)findViewById(R.id.edit_text_password);
        login=(Button)findViewById(R.id.button_login);
        sharedPreferences=getSharedPreferences(ARB, Context
                .MODE_PRIVATE);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name_=name.getText().toString();
                String pass=password.getText().toString();

                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString(NAME,name_); //NAME IS THE KEY VALUE
                editor.putString(PASSWORD,pass);
                editor.commit();

                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });
    }
}

Main2Activity.java

package com.example.user.store_session;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Main2Activity extends AppCompatActivity {
    Button logout,close;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        logout=(Button)findViewById(R.id.logout_button);
        close=(Button)findViewById(R.id.close_button);

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences sharedpreferences = getSharedPreferences(MainActivity.ARB, Context
                        .MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.clear();
                editor.commit();

            }
        });
    }

}

应用程序关闭并重新打开后,我没有收到第二个活动。

1 个答案:

答案 0 :(得分:0)

在MainActivity.java中,只需检查值NAME或PASSWORD是否存在。

MainActivity.java

package com.example.user.store_session;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.session.MediaSessionManager;
import android.os.UserManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    EditText name,password;
    Button login;
       public static final String ARB="ARBAZ";
       public static final String NAME="USERNAME";
    public static final String PASSWORD="PASSWORD";
       SharedPreferences sharedPreferences;


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

        name=(EditText)findViewById(R.id.Edit_text_name);
        password=(EditText)findViewById(R.id.edit_text_password);
        login=(Button)findViewById(R.id.button_login);
        sharedPreferences=getSharedPreferences(ARB, Context
                .MODE_PRIVATE);
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String name_=name.getText().toString();
                String pass=password.getText().toString();

                SharedPreferences.Editor editor=sharedPreferences.edit();
                editor.putString(NAME,name_); //NAME IS THE KEY VALUE
                editor.putString(PASSWORD,pass);
                editor.commit();

                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
            }
        });

           // check if NAME have a value. If have value, user has been logged, so go to the second activity.
          if ( !sharedPreferences.getString("NAME","").equals("") || !sharedPreferences.getString("PASSWORD","").equals("") ){
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                startActivity(intent);
           }

    }
}