如何在第一次运行后跳过第二次活动?

时间:2013-10-04 08:20:44

标签: java android android-intent timer

我正在处理一个包含SplashScreen.java我的第一个活动的应用。之后它的节目LoginActivity.java用于登录。我的LoginActivity.java班最终开始SplashActivity.java。我希望每次登录后第一次启动我的应用SplashScreen.java来电SplashActivity.java而不是LoginActivity.java。为此,我在我的SplashScreen.java课程中做了一些更改,但它没有正常工作。

SplashScreen.java类 -

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);
    }
}

其他人帮助我。现在是第一次运行后唯一的问题。应用从SplashActivity开始,而不是从SplashScreen开始。

7 个答案:

答案 0 :(得分: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();
                }
           }
    }

答案 1 :(得分:2)

  

步骤1:在您的登录类

中声明此信息
SharedPreferences pref;
SharedPreferences.Editor editor;
  

第2步:声明onCrete方法

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

步骤3:点击登录页面中的提交按钮,粘贴下面的行   :

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

步骤4:当用户凭证和数据库凭据匹配时,将其放在获取成功消息的位置。

editor.putString("selected", "TRUE");
editor.commit();

答案 2 :(得分:1)

@John检查,并参考这个工作代码,我希望,它可以帮助你,

    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();

    }
};

答案 3 :(得分:0)

我认为那里存在逻辑问题。如果没有执行,则运行它并标记为已执行。

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();
}

答案 4 :(得分:0)

问题是调用finish并不会阻止onCreate的其余部分被执行。您需要在以下后面添加一个return语句:

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

答案 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);
        }

    }

答案 6 :(得分:0)

你错过了我希望它会解决你的问题

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);




        }

    }
}
相关问题