不直接重定向到第三页

时间:2017-05-29 05:57:56

标签: android sharedpreferences

我有三页1.SplashScreen 2.Welcome 3.MainActivity。我想如果用户第一次进入,那么他必须按顺序进入Splashscreen-Welcome-MainActivity.But如果用户第二次进入或以后他必须去直接从SplashScreen到MainActivity。问题是,即使用户第二次进入,他的顺序与第一次相同,也不会直接重定向到MainActivity。以下是我的代码 -

SplashScreen.java

public class SplashScreen extends AppCompatActivity {


    private static int SPLASH_TIME_OUT = 3000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        final MainActivity login=new MainActivity(getApplicationContext());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity

                if (login.isuserLoggedIn()) {

                    startActivity(new Intent(SplashScreen.this,MainActivity.class));
                    finish();
                }
                else {

                    startActivity(new Intent(SplashScreen.this,Welcome.class));
                    finish();
                }


            }
        }, SPLASH_TIME_OUT);
    }
}

Welcome.java

public class Welcome extends Activity{


TextView txt;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.welcome);
        txt=(TextView)findViewById(R.id.welcome);
        button=(Button)findViewById(R.id.btn);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(Welcome.this,MainActivity.class);
                startActivity(intent);
            }
        });


    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    public static final String IS_User_login = "isuserloggedin";
    static  SharedPreferences app_preferences;
    SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the app's shared preferences

        app_preferences =
                PreferenceManager.getDefaultSharedPreferences(this);

        // Get the value for the run counter
        int counter = app_preferences.getInt("counter", 0);

        // Update the TextView
        TextView text = (TextView) findViewById(R.id.text);
        text.setText("This app has been started " + counter + " times.");

        // Increment the counter
        editor = app_preferences.edit();
        editor.putInt("counter", ++counter);
        editor.commit(); // Very important

    }

    public  MainActivity(final Context applicationContext) {


        app_preferences = applicationContext.getSharedPreferences("usersession",0);

        //Edit pfeff file
        editor = app_preferences.edit();

        editor.apply();
    }

    public MainActivity()
    {

    }

    public static boolean isuserLoggedIn() {
        return app_preferences.getBoolean(IS_User_login,false);
    }

}

4 个答案:

答案 0 :(得分:1)

首先,您不需要任何静态成员来获取存储的共享首选项。其次,你试图得到你没有存储的共享偏好值。

简单地说,在你的启动活动中添加它

JSONObject jsonObject = new JSONObject(response);
JSONObject data = jsonObject.getJSONObject("data");//Get Data object
 int openingBalance = data.getInt("opening_balance");//Get opening balance

答案 1 :(得分:0)

像这样改变:

  public class SplashScreen extends AppCompatActivity {



public static final String PREFS_NAME = "MyPrefsFile";
    private static int SPLASH_TIME_OUT = 3000;
SharedPreferences settings = getSharedPreferences(SplashScreen.PREFS_NAME, 0); 
SharedPreferences.Editor editor = settings.edit();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        final MainActivity login=new MainActivity(getApplicationContext());
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                // This method will be executed once the timer is over
                // Start your app main activity
       boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
                if (hasLoggedIn) {

                    startActivity(new Intent(SplashScreen.this,MainActivity.class));
                    finish();
                }
                else {
//Set "hasLoggedIn" to true first time
editor.putBoolean("hasLoggedIn", true);
                    startActivity(new Intent(SplashScreen.this,Welcome.class));
                    finish();
                }


            }
        }, SPLASH_TIME_OUT);
    }
}

答案 2 :(得分:0)

    public class SplashScreen extends AppCompatActivity {

     boolean flag=false;
        private static int SPLASH_TIME_OUT = 3000;

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




    SharedPreferences pref = 
    getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();

        flag=pref.getBoolean("flag", false);







            final MainActivity login=new 
           MainActivity(getApplicationContext());
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    // This method will be executed once the timer is over
                    // Start your app main activity

                    if (login.isuserLoggedIn()) {



        if(!flag)
            {
    flag=true;
    editor.putBoolean("flag", flag);
                editor.commit(); // commit changes

    startActivity(new Intent(SplashScreen.this,MainActivity.class));
                        finish();

           }else
{
   startActivity(new Intent(SplashScreen.this,Welcome.class));
                        finish();

}




                    }
                 //   else {

    //                    startActivity(new //Intent(SplashScreen.this,Welcome.class));
     //                   finish();
             //       }


                }
            }, SPLASH_TIME_OUT);
        }
    }

答案 3 :(得分:0)

首先,我建议你不要使用匿名处理程序。 好像我们关闭了应用程序,处理程序仍然运行,它将使应用程序崩溃。 因此,使用一个处理程序实例,因此您可以在后退和取消方法中取消它。 您可以使用共享首选项来保存状态。