无法在if语句中启动活动?

时间:2016-05-31 13:23:31

标签: android start-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);
        }

这是我使用正常流程进行下一个活动的方式:

Intent intent = new Intent(this, GetUserInformation.class);
        e.putInt("activity", 2);
        e.commit();
        intent.putExtra("user_id",user_string);
        startActivity(intent);

第一个被忽略,第二个被调用,if条件很好,日志语句证明活动状态(即用户所在的位置)被正确记录。唯一的问题是意图不是叫,我不知道为什么。

欢迎任何帮助。感谢

额外代码: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);
    }

这是第一个在打开应用程序后调用的活动,它通常会转到下一个活动getUserInformation,但是如果用户已经登录并使用了该应用程序,则应该查看用户的最后位置并转到那里。 / p>

我使用数字保存状态的方式参考每个活动,正如您在切换案例中看到的那样(保存在首选项文件中),不幸的是从if内部调用任何活动似乎对我有效。

1 个答案:

答案 0 :(得分:1)

正如所建议的,这是我的答案。首先,我觉得你的代码实际上是杂乱无章的 - 你应该花一些时间来清理它 - 最重要的是,你应该将大部分代码移到一些辅助函数中。另外,请删除使用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);
    }