2013-10-06 36 views
0

我知道如何获得启动当前活动的意图,但我应该如何构建我的代码,以便如果用户从登录页面进入,就会发生一件事,如果它们来自注册页面,会发生另一件事情?根据父活动执行不同的操作?

class Login extends Activity { 
public final static String EXTRA_MESSAGE = "net.asdqwe.activities.Login.EXTRA_MESSAGE"; 

//code here 

public void onClick(View arg0) { 
     Intent sendLoggedInUserToHomePage = new Intent(getApplicationContext(), Home.class); 
     sendLoggedInUserToHomePage.putExtra(EXTRA_MESSAGE,userEmailLoginPage); 
     startActivity(sendLoggedInUserToHomePage); 
    } 

} 


} 

ASD

class Signup extends Activity { 
public final static String EXTRA_MESSAGE = "net.asdqwe.activities.Signup.EXTRA_MESSAGE"; 

//code here 

public void onClick(View arg0) { 
     Intent signupSuccessHome = new Intent(getApplicationContext(), Home.class); 
     signupSuccessHome.putExtra(EXTRA_MESSAGE, userEmail); 
     startActivity(signupSuccessHome); 
    } 
} 

而现在我们是在家庭类,我不知道该怎么办。 到现在为止,我只有在注册页面上,所以很容易:

Intent loggedInUser = getIntent(); 
userEmailId = loggedInUser.getStringExtra(Signup.EXTRA_MESSAGE); 
userInfo = dbTools.getUserInfo(userEmailId); 

但我怎么现在,我有用户登录以及未来改变这种代码?

+1

为什么不通过意图传递值到您的活动? –

+0

我不明白你的意思。 –

+1

看看一个点3 http://www.vogella.com/articles/AndroidIntent/article.html –

回答

3

添加下面的代码在你家类

String reqFrom = ""; 

Bundle b = this.getIntent().getExtras(); 

if (b != null) 
reqFrom = b.getString("reqFrom"); 

if(reqFrom.equalsIgnoreCase("login")){ 
// some action 
} 
else { 
// some other action 
} 

添加下面的代码在你的登录页面。

Intent sendLoggedInUserToHomePage = new Intent(getApplicationContext(), Home.class); 
i.putExtra("reqFrom", "login"); 
startActivity(sendLoggedInUserToHomePage); 
+0

这看起来很有希望。将试试 –

+1

这就是我15分钟前提示你的方式 –

+0

我本来应该放在Home类中的代码就是我主要需要的:)。我投了你的意见 –

相关问题