无法在 if 语句中启动 activity?
Not able to start activity within if statement?
我的应用程序中有正常的活动流程。如果出现应用程序崩溃/用户强行关闭应用程序的情况,我会确保应用程序转到用户上次所在的前一个 activity。我在首选项文件中记录了这一点,当我的 activity 第一次启动时,我确保用户没有登录,如果他登录了,我会进入该文件以查看他上次登录的位置,然后直接转到那里。
问题是它似乎不起作用我尝试了以下变体
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
startActivity(i);
Intent i = new Intent(this, MainQuestionsActivity.class);
startActivity(i);
完整代码如下:
boolean meow = instance_user.mKinveyClient.user().isUserLoggedIn();
Log.i("main activity", "meow = " + meow);
// if already logged
if(meow){
Log.i(TAG, "(not first time): " + instance_user.mKinveyClient.user().getId());
user_text.setText("user id is " + instance_user.mKinveyClient.user().getId());
int a = s_logged.getInt("activity", 2);
Log.d(TAG, "activity going to :" + a);
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
// switch(a){
// case 1: i = new Intent(this, MainActivity.class);break;
// case 2: i = new Intent(this, ProfilingFeaturesActivity.class);break;
// case 4: i = new Intent(this, ProfilingSensorsActivity.class);break;
// case 5: i = new Intent(this, ProfilingDataCollectorsActivity.class);break;
// case 6: i = new Intent(this, ProfilingContextsActivity.class);break;
// case 7: i = new Intent(this, QuestionsActivity.class);break;
// case 8: i = new Intent(this, PauseActivity.class);break;
// case 9: i = new Intent(this, MainQuestionsActivity.class);break;
// }
startActivity(i);
}
这就是我以正常流程进入下一个 activity 的方式:
Intent intent = new Intent(this, GetUserInformation.class);
e.putInt("activity", 2);
e.commit();
intent.putExtra("user_id",user_string);
startActivity(intent);
第一个总是被忽略,第二个意图被调用,如果条件很好,日志语句证明 activity 状态(即用户所在的位置)是正确的 recorded.The唯一的问题是没有调用意图,我不知道为什么。
欢迎任何帮助。谢谢
额外代码:MainActivity.java
public class MainActivity extends AppCompatActivity {
StoreDbHelper db = null;
private static String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseInstance d = new DatabaseInstance(this);
db = DatabaseInstance.db;
// do not let screen switch off
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final SharedPreferences s_logged = getSharedPreferences("logged", Context.MODE_PRIVATE);
final SharedPreferences.Editor e = s_logged.edit();
int temp = s_logged.getInt("activity", 0);
Log.d("main activity", " s_logged activity ="+temp );
UserInstanceClass instance_user = new UserInstanceClass();
//final Client mKinveyClient = new Client.Builder("kid_W1EFbeKyy-", "1b6f09e812114210ae4447f310b38a0a"
// , this.getApplicationContext()).build();
instance_user.setmKinveyClient(new Client.Builder(this.getApplicationContext()).build());
//final Client mKinveyClient = new Client.Builder(this.getApplicationContext()).build();
// get app properties from kinvey.properties
final TextView user_text;
user_text = (TextView) findViewById(R.id.user_id);
// check if new to app
boolean meow = instance_user.mKinveyClient.user().isUserLoggedIn();
Log.i("main activity", "meow = " + meow);
int is_logged = s_logged.getInt("logged",0);
Log.d("main activity", " s_logged logged ="+is_logged);
// if already logged
if(meow){
Log.i(TAG, "(not first time): " + instance_user.mKinveyClient.user().getId());
user_text.setText("user id is " + instance_user.mKinveyClient.user().getId());
int a = s_logged.getInt("activity", 2);
Log.d(TAG, "activity going to :" + a);
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
// switch(a){
// case 1: i = new Intent(this, MainActivity.class);break;
// case 2: i = new Intent(this, ProfilingFeaturesActivity.class);break;
// case 4: i = new Intent(this, ProfilingSensorsActivity.class);break;
// case 5: i = new Intent(this, ProfilingDataCollectorsActivity.class);break;
// case 6: i = new Intent(this, ProfilingContextsActivity.class);break;
// case 7: i = new Intent(this, QuestionsActivity.class);break;
// case 8: i = new Intent(this, PauseActivity.class);break;
// case 9: i = new Intent(this, MainQuestionsActivity.class);break;
// }
startActivity(i);
}
// if user is logged in meow = True dont log him in again!
if (!meow) {
instance_user.mKinveyClient.user().login(new KinveyUserCallback() {
@Override
public void onFailure(Throwable error) {
Log.i(TAG, "Fail");
user_text.setText("User not identified !!");
Log.i(TAG, "" + error);
}
@Override
public void onSuccess(User result) {
user_text.setText("user id is " + result.getId());
e.putInt("logged",1); // means it is logged in
e.commit();
Log.i(TAG, "Logged in a new implicit user with id(first time): " + result.getId());
}
});
}
String user_string = instance_user.mKinveyClient.user().getId();
Log.i(TAG, "running for first time");
// Reset preferences file when installing application for the first time
// remove only debugging!
SharedPreferences s = getSharedPreferences("bid_window_values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = s.edit();
editor.putInt("current_question_number",0);
editor.putInt("current_day",1);
editor = AddDouble.putDouble(editor,"current_credit",0);
editor = AddDouble.putDouble(editor,"current_privacy",100);
editor.commit();
// clear content to simulate first time user
db.removeAll();
Intent intent = new Intent(this, GetUserInformation.class);
e.putInt("activity", 2);
e.commit();
intent.putExtra("user_id",user_string);
startActivity(intent);
}
这是打开应用程序后调用的第一个 activity,它通常会转到下一个 activity getUserInformation,但如果用户已经登录并使用了该应用程序,它应该会看到用户上次去的地方。
我使用引用每个 activity 的数字保存状态的方式正如您在 switch 案例中看到的那样(保存在首选项文件中),不幸的是从 if 内部调用任何 activity似乎对我有用。
按照建议,这是我的答案。首先,我觉得你的代码真的很杂乱——你应该花一些时间来清理它——最重要的是,你应该把大部分代码移到一些辅助函数中。另外,请删除所有使用 SharedPreferences 的代码——我真的不明白你为什么有这段代码。
请参阅下面我建议的更改,希望可以帮助您解决问题:
public class MainActivity extends AppCompatActivity {
StoreDbHelper db = null;
SharedPreferences.Editor s_loggedEditor = null;
private static String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseInstance d = new DatabaseInstance(this);
db = DatabaseInstance.db;
// do not let screen switch off
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
UserInstanceClass instance_user = new UserInstanceClass();
instance_user.setmKinveyClient(new Client.Builder(this.getApplicationContext()).build());
final TextView user_text;
user_text = (TextView) findViewById(R.id.user_id);
// check if new to app
boolean meow = instance_user.mKinveyClient.user().isUserLoggedIn();
Log.i("main activity", "meow = " + meow);
// if already logged
if(meow){
Log.i(TAG, "(not first time): " + instance_user.mKinveyClient.user().getId());
user_text.setText("user id is " + instance_user.mKinveyClient.user().getId());
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i);
}
else{
//instead of if (!meow){}...just use ELSE
instance_user.mKinveyClient.user().login(new KinveyUserCallback() {
@Override
public void onFailure(Throwable error) {
Log.i(TAG, "Fail");
user_text.setText("User not identified !!");
Log.i(TAG, "" + error);
}
@Override
public void onSuccess(User result) {
user_text.setText("user id is " + result.getId());
Log.i(TAG, "Logged in a new implicit user with id(first time): " + result.getId());
//now you can do whatever you wanted to do when the user logs in for the first time
processFirstTimeLogin();
//Here I am just assuming you wanted to show the GetUserInformation Activity
String user_string = result.getId();
Log.i(TAG, "running for first time");
showGetUserInformationIntent(user_string);
}
});
}
}
//I just moved some of the code here
private void processFirstTimeLogin(){
//THE REST OF YOUR CODE
// Reset preferences file when installing application for the first time
// remove only debugging!
SharedPreferences s = getSharedPreferences("bid_window_values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = s.edit();
editor.putInt("current_question_number",0);
editor.putInt("current_day",1);
editor = AddDouble.putDouble(editor,"current_credit",0);
editor = AddDouble.putDouble(editor,"current_privacy",100);
editor.commit();
// clear content to simulate first time user
db.removeAll();
}
//Here you show the other Intent
private void showGetUserInformationIntent(String userString){
Intent intent = new Intent(this, GetUserInformation.class);
s_loggedEditor.putInt("activity", 2);
s_loggedEditor.commit();
intent.putExtra("user_id",userString);
startActivity(intent);
}
我的应用程序中有正常的活动流程。如果出现应用程序崩溃/用户强行关闭应用程序的情况,我会确保应用程序转到用户上次所在的前一个 activity。我在首选项文件中记录了这一点,当我的 activity 第一次启动时,我确保用户没有登录,如果他登录了,我会进入该文件以查看他上次登录的位置,然后直接转到那里。
问题是它似乎不起作用我尝试了以下变体
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
startActivity(i);
Intent i = new Intent(this, MainQuestionsActivity.class);
startActivity(i);
完整代码如下:
boolean meow = instance_user.mKinveyClient.user().isUserLoggedIn();
Log.i("main activity", "meow = " + meow);
// if already logged
if(meow){
Log.i(TAG, "(not first time): " + instance_user.mKinveyClient.user().getId());
user_text.setText("user id is " + instance_user.mKinveyClient.user().getId());
int a = s_logged.getInt("activity", 2);
Log.d(TAG, "activity going to :" + a);
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
// switch(a){
// case 1: i = new Intent(this, MainActivity.class);break;
// case 2: i = new Intent(this, ProfilingFeaturesActivity.class);break;
// case 4: i = new Intent(this, ProfilingSensorsActivity.class);break;
// case 5: i = new Intent(this, ProfilingDataCollectorsActivity.class);break;
// case 6: i = new Intent(this, ProfilingContextsActivity.class);break;
// case 7: i = new Intent(this, QuestionsActivity.class);break;
// case 8: i = new Intent(this, PauseActivity.class);break;
// case 9: i = new Intent(this, MainQuestionsActivity.class);break;
// }
startActivity(i);
}
这就是我以正常流程进入下一个 activity 的方式:
Intent intent = new Intent(this, GetUserInformation.class);
e.putInt("activity", 2);
e.commit();
intent.putExtra("user_id",user_string);
startActivity(intent);
第一个总是被忽略,第二个意图被调用,如果条件很好,日志语句证明 activity 状态(即用户所在的位置)是正确的 recorded.The唯一的问题是没有调用意图,我不知道为什么。
欢迎任何帮助。谢谢
额外代码:MainActivity.java
public class MainActivity extends AppCompatActivity {
StoreDbHelper db = null;
private static String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseInstance d = new DatabaseInstance(this);
db = DatabaseInstance.db;
// do not let screen switch off
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final SharedPreferences s_logged = getSharedPreferences("logged", Context.MODE_PRIVATE);
final SharedPreferences.Editor e = s_logged.edit();
int temp = s_logged.getInt("activity", 0);
Log.d("main activity", " s_logged activity ="+temp );
UserInstanceClass instance_user = new UserInstanceClass();
//final Client mKinveyClient = new Client.Builder("kid_W1EFbeKyy-", "1b6f09e812114210ae4447f310b38a0a"
// , this.getApplicationContext()).build();
instance_user.setmKinveyClient(new Client.Builder(this.getApplicationContext()).build());
//final Client mKinveyClient = new Client.Builder(this.getApplicationContext()).build();
// get app properties from kinvey.properties
final TextView user_text;
user_text = (TextView) findViewById(R.id.user_id);
// check if new to app
boolean meow = instance_user.mKinveyClient.user().isUserLoggedIn();
Log.i("main activity", "meow = " + meow);
int is_logged = s_logged.getInt("logged",0);
Log.d("main activity", " s_logged logged ="+is_logged);
// if already logged
if(meow){
Log.i(TAG, "(not first time): " + instance_user.mKinveyClient.user().getId());
user_text.setText("user id is " + instance_user.mKinveyClient.user().getId());
int a = s_logged.getInt("activity", 2);
Log.d(TAG, "activity going to :" + a);
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
// switch(a){
// case 1: i = new Intent(this, MainActivity.class);break;
// case 2: i = new Intent(this, ProfilingFeaturesActivity.class);break;
// case 4: i = new Intent(this, ProfilingSensorsActivity.class);break;
// case 5: i = new Intent(this, ProfilingDataCollectorsActivity.class);break;
// case 6: i = new Intent(this, ProfilingContextsActivity.class);break;
// case 7: i = new Intent(this, QuestionsActivity.class);break;
// case 8: i = new Intent(this, PauseActivity.class);break;
// case 9: i = new Intent(this, MainQuestionsActivity.class);break;
// }
startActivity(i);
}
// if user is logged in meow = True dont log him in again!
if (!meow) {
instance_user.mKinveyClient.user().login(new KinveyUserCallback() {
@Override
public void onFailure(Throwable error) {
Log.i(TAG, "Fail");
user_text.setText("User not identified !!");
Log.i(TAG, "" + error);
}
@Override
public void onSuccess(User result) {
user_text.setText("user id is " + result.getId());
e.putInt("logged",1); // means it is logged in
e.commit();
Log.i(TAG, "Logged in a new implicit user with id(first time): " + result.getId());
}
});
}
String user_string = instance_user.mKinveyClient.user().getId();
Log.i(TAG, "running for first time");
// Reset preferences file when installing application for the first time
// remove only debugging!
SharedPreferences s = getSharedPreferences("bid_window_values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = s.edit();
editor.putInt("current_question_number",0);
editor.putInt("current_day",1);
editor = AddDouble.putDouble(editor,"current_credit",0);
editor = AddDouble.putDouble(editor,"current_privacy",100);
editor.commit();
// clear content to simulate first time user
db.removeAll();
Intent intent = new Intent(this, GetUserInformation.class);
e.putInt("activity", 2);
e.commit();
intent.putExtra("user_id",user_string);
startActivity(intent);
}
这是打开应用程序后调用的第一个 activity,它通常会转到下一个 activity getUserInformation,但如果用户已经登录并使用了该应用程序,它应该会看到用户上次去的地方。
我使用引用每个 activity 的数字保存状态的方式正如您在 switch 案例中看到的那样(保存在首选项文件中),不幸的是从 if 内部调用任何 activity似乎对我有用。
按照建议,这是我的答案。首先,我觉得你的代码真的很杂乱——你应该花一些时间来清理它——最重要的是,你应该把大部分代码移到一些辅助函数中。另外,请删除所有使用 SharedPreferences 的代码——我真的不明白你为什么有这段代码。
请参阅下面我建议的更改,希望可以帮助您解决问题:
public class MainActivity extends AppCompatActivity {
StoreDbHelper db = null;
SharedPreferences.Editor s_loggedEditor = null;
private static String TAG = MainActivity.class.getName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DatabaseInstance d = new DatabaseInstance(this);
db = DatabaseInstance.db;
// do not let screen switch off
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
UserInstanceClass instance_user = new UserInstanceClass();
instance_user.setmKinveyClient(new Client.Builder(this.getApplicationContext()).build());
final TextView user_text;
user_text = (TextView) findViewById(R.id.user_id);
// check if new to app
boolean meow = instance_user.mKinveyClient.user().isUserLoggedIn();
Log.i("main activity", "meow = " + meow);
// if already logged
if(meow){
Log.i(TAG, "(not first time): " + instance_user.mKinveyClient.user().getId());
user_text.setText("user id is " + instance_user.mKinveyClient.user().getId());
Intent i = new Intent(MainActivity.this, com.example.ramapriyasridharan.trialapp04.MainQuestionsActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
startActivity(i);
}
else{
//instead of if (!meow){}...just use ELSE
instance_user.mKinveyClient.user().login(new KinveyUserCallback() {
@Override
public void onFailure(Throwable error) {
Log.i(TAG, "Fail");
user_text.setText("User not identified !!");
Log.i(TAG, "" + error);
}
@Override
public void onSuccess(User result) {
user_text.setText("user id is " + result.getId());
Log.i(TAG, "Logged in a new implicit user with id(first time): " + result.getId());
//now you can do whatever you wanted to do when the user logs in for the first time
processFirstTimeLogin();
//Here I am just assuming you wanted to show the GetUserInformation Activity
String user_string = result.getId();
Log.i(TAG, "running for first time");
showGetUserInformationIntent(user_string);
}
});
}
}
//I just moved some of the code here
private void processFirstTimeLogin(){
//THE REST OF YOUR CODE
// Reset preferences file when installing application for the first time
// remove only debugging!
SharedPreferences s = getSharedPreferences("bid_window_values", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = s.edit();
editor.putInt("current_question_number",0);
editor.putInt("current_day",1);
editor = AddDouble.putDouble(editor,"current_credit",0);
editor = AddDouble.putDouble(editor,"current_privacy",100);
editor.commit();
// clear content to simulate first time user
db.removeAll();
}
//Here you show the other Intent
private void showGetUserInformationIntent(String userString){
Intent intent = new Intent(this, GetUserInformation.class);
s_loggedEditor.putInt("activity", 2);
s_loggedEditor.commit();
intent.putExtra("user_id",userString);
startActivity(intent);
}