1

I am working on one app which contain SplashScreen.java my first activity. After that its show LoginActivity.java for login. And my LoginActivity.java class eventually start SplashActivity.java. I want after login first time everytime i start my app SplashScreen.java calls SplashActivity.java instead of LoginActivity.java. For this i made some changes in my SplashScreen.java class but its not working fine.

SplashScreen.java Class-

public class SplashScreen extends Activity 
{
    private long splashDelay = 5000; //5 seconds
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        if(pref.getBoolean("activity_executed", false))
        {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        } 
        else 
        {
            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();
        }
        TimerTask task = new TimerTask()
        {

            @Override
            public void run() {
                finish();
                Intent mainIntent = new Intent().setClass(SplashScreen.this, LoginActivity.class);
                startActivity(mainIntent);
            }

        };

        Timer timer = new Timer();
        timer.schedule(task, splashDelay);
    }
}

Anyone else help me. Now the only problem after first run. App start from SplashActivity instead of starting from SplashScreen.

John R
  • 2,078
  • 8
  • 35
  • 58

7 Answers7

2
     @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.splash_screen);
            SharedPreferences pref = getSharedPreferences("ActivityPREF",  Context.MODE_PRIVATE);
            Editor ed = pref.edit();
     Boolean Exist=pref.getBoolean("activity_executed", false);// Check is user logged in or not
            if(Exist)// if allready logged in then forward it to Splash Activity
            {

                Intent intent = new Intent(this, SplashActivity.class);
                startActivity(intent);
                finish();
            } 
            else // Not logged in
            {

               Handler handler = new Handler();
        handler.removeCallbacks(runnable);
        handler.postDelayed(runnable, 2000L);
             }
Runnable runnable = new Runnable() {
        public void run() {

            new AsyncParsing().execute();

        }
    };

    private class AsyncParsing extends AsyncTask<Void, Void, Void> {
        @Override
        protected void onPreExecute() {
            Log.d("Splash Activity", "In pre execute");

        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.d("Splash Activity", "In do in backgriund ");


            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            Log.d("Splash Activity", "In post execute");
                   Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
                startActivity(intent);
                finish();
                }
           }
    }
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
2

Step 1 : Declare this in ur login class

SharedPreferences pref;
SharedPreferences.Editor editor;

Step 2 :Declare In onCrete method

pref = getSharedPreferences("login", MODE_PRIVATE);
editor = pref.edit();

Step 3 : OnClick of submit button in login page paste the lines below :

String check=pref.getString("selected", "nil")
if(check.equals("TRUE"))
{
Intent intent=new Intent(this,splash.class);
startActivity(intent);
}

Step 4: put this where u r getting the success message when user credentails and db credentials matches.

editor.putString("selected", "TRUE");
editor.commit();
KOTIOS
  • 11,177
  • 3
  • 39
  • 66
  • Please make this changes also with @patrioit answer. then your app perfect. thank you for this code Monica the great. – John R Oct 05 '13 at 10:09
  • the great check this http://stackoverflow.com/questions/4182761/finish-old-activity-and-start-a-new-one-or-vice-versa – John R Oct 05 '13 at 14:17
1

@John check and just refer this working code, i hope,it help u,

    setContentView(R.layout.main);
    pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
    Log.v("","onCreate is calling");
    if(pref.getBoolean("activity_executed", false))
    {
         Log.v("","Before if called");
        setContentView(R.layout.menu_frame);
         Log.v("","after if called");
         new Handler().postDelayed(csRunnable1, 3000);

    } 
    else 
    {
       new Handler().postDelayed(csRunnable2, 3000);  
        Editor ed = pref.edit();
        ed.putBoolean("activity_executed", true);
        ed.commit();

    }


}
Runnable csRunnable1=new Runnable() 
{       
    @Override
    public void run() 
    {
         Intent intent = new Intent(SplashScreen.this, SplashActivity.class);
            startActivity(intent);
            finish();

    }
};
Runnable csRunnable2=new Runnable() 
 {      
    @Override
    public void run() 
    {
         Intent intent = new Intent(SplashScreen.this, LoginActivity.class);
            startActivity(intent);
            finish();

    }
};
rkv
  • 131
  • 6
  • after so many changes your answer is working completely fine. need your one more little help. when you back text me here. Thank you for your answer. – John R Oct 04 '13 at 12:11
0

I would think you have a logic problem there. Should be if not executed, run it and mark as executed.

if(!pref.getBoolean("activity_executed", false)) {
    Editor ed = pref.edit();
    ed.putBoolean("activity_executed", true);
    ed.commit();
    Intent intent = new Intent(this, SplashActivity.class);
    startActivity(intent);
    finish();
}
Lionel Port
  • 3,492
  • 23
  • 26
0

The problem is that calling finish does not stop the rest of onCreate from being executed. You need to put a return statement after:

Intent intent = new Intent(this, SplashActivity.class);
startActivity(intent);
finish();
Leif
  • 26
  • 5
0
// try this
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        SharedPreferences pref = getSharedPreferences("ActivityPREF", Context.MODE_PRIVATE);
        if(pref.getBoolean("activity_executed", false))
        {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        }
        else
        {
            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();
            Timer timer = new Timer();
            TimerTask task = new TimerTask()
            {

                @Override
                public void run() { 
                    timer.cancel();
                    Intent mainIntent = new Intent().setClass(SplashScreen.this, LoginActivity.class);
                    startActivity(mainIntent);
                    finish();
                }

            };

            timer.schedule(task, splashDelay);
        }

    }
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
  • i've tried your code. but next time my app start its start from SplashActicity not from SplashScreen. – John R Oct 04 '13 at 09:00
0

You missing I hope It will solve your problem

public class SplashScreen extends Activity 
{private long splashDelay = 5000; // 5 seconds

    /** Called when the activity is first created. */
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            finish();
            Intent mainIntent = new Intent().setClass(Test.this,
                    LoginActivity.class);
            startActivity(mainIntent);
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_screen);
        SharedPreferences pref = getSharedPreferences("ActivityPREF",
                Context.MODE_PRIVATE);

        if (pref.getBoolean("activity_executed", false)) {
            Intent intent = new Intent(this, SplashActivity.class);
            startActivity(intent);
            finish();
        } else {

            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();



            Timer timer = new Timer();
            timer.schedule(task, splashDelay);




        }

    }
}
Prabhunath Yadav
  • 185
  • 1
  • 14
  • i've tried your code. but next time my app start its start from SplashActicity not from SplashScreen – John R Oct 04 '13 at 09:08