在onCreate()中启动intent时获取无限循环

时间:2017-04-18 08:42:58

标签: java android retrofit2

我是Android新手,在实施时遇到了问题。 我试图将用户导航到MainActivity.java活动(如果用户已经登录),如果用户尚未登录,那么它将导航到LoginActivity.java。

这是我的MainActivity.java的onCreate()方法中的代码。

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

    name = (TextView) findViewById(R.id.tvName);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    Gson gson = new GsonBuilder().create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    sponsorApi = retrofit.create(SponsorInterface.class);

    if (prefs.getBoolean(Constants.IS_LOGGED_IN, false)){
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
        user = prefs.getString(Constants.USER, "Username");
        Call<Sponsor> sponsorRequest = sponsorApi.getSponsor(user);
        sponsorRequest.enqueue(new Callback<Sponsor>() {
            @Override
            public void onResponse(Call<Sponsor> call, Response<Sponsor> response) {
                int statusCode = response.code();
                final Sponsor sponsor = response.body();
                name.setText(sponsor.getName());
                Log.d("SponsorDetails Activity", "onResponse: " + statusCode);
            }

            @Override
            public void onFailure(Call<Sponsor> call, Throwable t) {
                Log.d("LoginActivity", "onFailure: " + t.getLocalizedMessage());
            }
        });

    }else {
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        Toast.makeText(getBaseContext(), "Please login to proceed", Toast.LENGTH_SHORT).show();
        startActivity(intent);
    }
}

当我运行应用程序时,它可以运行,但是Activity会运行到无限循环中,我不知道是什么原因造成了循环。

Update1:​​调试程序 enter image description here PS:图片中的SponsorDetails.java是问题的MainActivity.java。

Update2:删除MainActivity的意图 正如我删除的评论中所建议的那样

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

但我仍然在onResponse()方法中获得无限循环。是的,删除代码后,我从模拟器中卸载了应用程序,并从IDE重新运行了应用程序。

更新3:onStart()方法

@Override
protected void onStart() {
    super.onStart();

    if (prefs.getBoolean(Constants.IS_LOGGED_IN, false)){
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
    }else{
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        Toast.makeText(getBaseContext(), "Please login to proceed", Toast.LENGTH_SHORT).show();
        startActivity(intent); 
    }
}

4 个答案:

答案 0 :(得分:1)

如果条件允许,您正在以递归方式呼叫 MainActivity 因此,当条件为真时,会一次又一次地调用MainActivity。

删除两行

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

或在if条件下更改您要转到的活动名称。

答案 1 :(得分:0)

基本上,你的主要活动一直在开始,而这些是导致它的两行代码。只需删除它们。

e,o,u

答案 2 :(得分:0)

将此onStart()替换为此

@Override
protected void onStart() {
    super.onStart();
    if (!prefs.getBoolean(Constants.IS_LOGGED_IN, false)){
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        Toast.makeText(getBaseContext(), "Please login to proceed", Toast.LENGTH_SHORT).show();
        startActivity(intent); 
    }
}

请告诉我这是否有帮助?

答案 3 :(得分:-1)

我认为您没有更新偏好设置中的登录状态

更新状态如下

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

    name = (TextView) findViewById(R.id.tvName);
    prefs = PreferenceManager.getDefaultSharedPreferences(this);

    Gson gson = new GsonBuilder().create();
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    sponsorApi = retrofit.create(SponsorInterface.class);

    if (prefs.getBoolean(Constants.IS_LOGGED_IN, false)){
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
        user = prefs.getString(Constants.USER, "Username");
        Call<Sponsor> sponsorRequest = sponsorApi.getSponsor(user);
        sponsorRequest.enqueue(new Callback<Sponsor>() {
            @Override
            public void onResponse(Call<Sponsor> call, Response<Sponsor> response) {
                int statusCode = response.code();
                final Sponsor sponsor = response.body();
                name.setText(sponsor.getName());
                Log.d("SponsorDetails Activity", "onResponse: " + statusCode);

                // update the IS_LOGGED_IN status as true
                SharedPreferences.Editor editor = pref.edit();
                editor.putString(Constants.IS_LOGGED_IN ,true);
                editor.commit();

            }

            @Override
            public void onFailure(Call<Sponsor> call, Throwable t) {
                Log.d("LoginActivity", "onFailure: " + t.getLocalizedMessage());
            }
        });

    }else {
        Intent intent = new Intent(MainActivity.this, LoginActivity.class);
        Toast.makeText(getBaseContext(), "Please login to proceed", Toast.LENGTH_SHORT).show();
        startActivity(intent);
    }
}

请试一试。这将解决您的问题