检查Intent是否存在

时间:2016-02-29 10:16:09

标签: android android-intent

在我的程序中,用户从他们使用应用程序的欢迎页面开始,需要输入详细信息才能继续。当他们到达主页时,他们将输入一个名称,并将其作为意图传递到主页。

我希望用户在第一次使用应用程序时被带到欢迎页面,然后在此之后一直被带到主页。

我将名称保存为共享首选项,因此它应始终存在。

所以我尝试的是始终将用户发送到主页,但如果没有Intent存在(即第一次),他们将被带到主页,但我无法使其工作。这是我的努力。

public class HomeActivity extends Activity {

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


        Intent intent1 = getIntent();
        Bundle bundle = intent1.getExtras();


         if( intent1.getExtras() == null)
            {
                Intent intent = new Intent(HomeActivity.this,Welcome.class);
                startActivity(intent);
            } 


        final String name = bundle.getString("Name");
        final int targetTime = bundle.getInt("targetTime", 1);

4 个答案:

答案 0 :(得分:1)

onCreate

Welcome.java中执行此操作
SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
String name = mPrefs.getString("Name", null);

if (name != null) {
   Intent intent = new Intent(Welcome.this,HomeActivity.class);
   startActivity(intent);
}

当您转移到共享偏好中的HomeActivity保存名称

SharedPreferences mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("name", name);
editor.commit();

答案 1 :(得分:1)

在欢迎活动的onCreate()内,检查共享首选项中是否存在名称。如果没有继续活动。如果名称存在,您可以启动主页并finish()欢迎活动。

确保在开始主页活动后致电finish(),否则当您在首页按回按钮时,您将被重定向到欢迎页面。

答案 2 :(得分:0)

<{1}} onCreate中的WelcomeActivity检查sharedPreferance的内容是否为空或设置为某个值,如果为空,请留在WelcomeActivity else {{ 1}}。并在startActivity(for_HomePage)

WelcomeActivity launcher

答案 3 :(得分:0)

String login="";
    
    Bundle extras=getIntent().getExtras();
    if (extras!=null) {
         login = getIntent().getStringExtra("login");
    }if (login==null){
        login="";
    }
    if(user==null  && !login.equalsIgnoreCase("login")){
        Intent contentIntent=new Intent(MainActivity.this,MarketActivity.class);
        startActivity(contentIntent);
        finish();
    }
相关问题