2016-10-05 9 views

答えて

0

SharedPreferencesと呼ばれます。あなたのアクセスコードをSharedPreferenceに保存し、アクセスコードがあるかどうか確認してください。その後、問題はありません。

SharedPreferences tutorial

はそれがお役に立てば幸いです。乾杯!

+1

私はあなたが答えが非常に有用であったことを発見しました –

+0

@DanielAnishchenko、welcome :) –

0

私は最も良い方法をお勧めします。あなたがそのようなものに執着しているときはいつでも、スプラッシュ画面があなたを助けます。スプラッシュ画面では、以前にログインしているかどうかを確認していて、それを使って別のアクティビティに移動しています。ただ、ランチャーの活動として、スプラッシュスクリーンを作る

- :

は私がどのように説明しましょう。ずっと長く表示したくない場合は、ハンドラを1〜2秒間実行してください。次に、下のコードを見てください。

SplashScreen.java

public class SplashScreen extends AppCompatActivity { 

    private String email; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.activity_splash); 

     //SharedPreference to Store API Result 
     SharedPreferences pref = getApplicationContext().getSharedPreferences("CachedResponse", 0); 
     SharedPreferences.Editor editor = pref.edit(); 
     editor.apply(); 

     email = pref.getString("login", null); 

     int SPLASH_TIME_OUT = 3000; 

     if (email != null) { 

      //It means User is already Logged in so I will take the user to Select_College Screen 

      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() { 

        Intent intent = new Intent(SplashScreen.this, Select_College.class); 
        intent.putExtra("Email", email); 
        startActivity(intent); 
        finish(); 

       } 

      }, SPLASH_TIME_OUT); 

     } else { 

      //It means User is not Logged in so I will take the user to Login Screen 

      new Handler().postDelayed(new Runnable() { 

       @Override 
       public void run() { 

        Intent intent = new Intent(SplashScreen.this, Login.class); 
        startActivity(intent); 
        finish(); 

       } 

      }, SPLASH_TIME_OUT); 

     } 

    } 
} 

Login.java

public class Login extends AppCompatActivity { 

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

     //SharedPreference to Store API Result 
     pref = getApplicationContext().getSharedPreferences("CachedResponse", 0); 

     Login(); 

    } 

    private void login() { 

     //If login is successfull, before moving to next activity, store something in sharedpreference with name login. It can be email or just a string as "true" 

     SharedPreferences.Editor editor = pref.edit(); 
         editor.putString("login", email); 
         editor.apply(); 

         Intent intent = new Intent(DoctorLogin.this, Select_Collage.class); 
         intent.putExtra("Email", email); 
         startActivity(intent); 
         finish(); 

    } 
} 

Select_Collage.java

public class Select_Collage extends AppCompatActivity { 

    private SharedPreferences pref; 

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

     //SharedPreference to Store API Result 
     pref = getApplicationContext().getSharedPreferences("CachedResponse", 0); 

     //Somewhere on Signout button click, delete the login sharedpreference 
     signOut(); 

    } 

    private void signOut() { 

     SharedPreferences.Editor editor = pref.edit(); 
     editor.remove("login"); 
     editor.apply(); 

     Intent intent = new Intent(Select_Collage.this, Login.class); 
     startActivity(intent); 
     finish(); 

    } 
} 

このようにしてこの問題を解決できます。コーディングを保つ:)

+0

私はこれに対して別の方法を使用しましたが、私は読み込み画面を作るためにスプラッシュスクリーンメソッドを使用しました。手伝い! –

関連する問題