意图发起错误的活动,我该如何解决?

时间:2018-07-29 10:13:08

标签: android

我想在更新电子邮件或用户帐户的密码后启动登录活动。问题是,当我单击更新按钮时,启动的活动是最后一个活动(MainActivity)。

Intent i=new Intent(getApplicationContext(),LoginActivity.class);
startActivity(i);

这是LoginActivity中onCreate()方法的代码:我不知道问题出在哪里,即使当我将另一个活动更改为启动时,也是同样的问题!

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        ClassLoginTv=findViewById(R.id.textView2);
        ClassLoginRl=findViewById(R.id.button12);
        ClassLoginEt1=findViewById(R.id.editTextm);
        ClassLoginEt2=findViewById(R.id.editText2);





        ClassLoginTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i2=new Intent(view.getContext(),RegisterActivity.class);
                startActivity(i2);
                overridePendingTransition(R.anim.flip_horizontal_in, R.anim.flip_horizontal_out);
            }});

         sharedPreferencesConfigs=new SharedPreferencesConfigs(getApplicationContext());
        if(sharedPreferencesConfigs.ReadLoginStatus()){

            startActivity(new Intent(getApplicationContext(),MainActivity.class));
            finish();

        }




    }



    public void ValidLogin(View view) {

        String email = ClassLoginEt1.getText().toString();
        String password = ClassLoginEt2.getText().toString();

        if (!Outils.isEmail(email))
            ClassLoginEt1.setError("enter a valid email address");

        else if (!Outils.isPassword(password))
            ClassLoginEt2.setError("between 4 and 10 alphanumeric characters");

        else {


            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(getString(R.string.BaseUrl))
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

            MyInterface myInterface=retrofit.create(MyInterface.class);

            Call<List<User>> call = myInterface.getLogin(email,  password);

            call.enqueue(new Callback<List<User>>() {
                @Override
                public void onResponse(Call<List<User>> call, Response<List<User>> response) {

                    List<User> UserList = response.body();
                    String s=UserList.get(0).getStatus();

                   if(s.equals("200"))  Toasty.error(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();

                   else if(s.equals("202"))  Toasty.error(getApplicationContext(),"mail not found",Toast.LENGTH_SHORT).show();

                   else if(s.equals("201")) Toasty.error(getApplicationContext(),"Password error",Toast.LENGTH_SHORT).show();

                   else {

                       Intent i1=new Intent(getApplicationContext(),MainActivity.class);
                       startActivity(i1);
                       overridePendingTransition(R.anim.appear_top_left_in, R.anim.appear_top_left_out);


                       Toasty.success(getApplicationContext(), "Success!", Toast.LENGTH_SHORT, true).show();


                       SharedPreferences sharedPref=getSharedPreferences("MyPref", Context.MODE_PRIVATE);
                       SharedPreferences.Editor editor=sharedPref.edit();
                       editor.putInt("Id",UserList.get(0).getId());
                       editor.putString("Noun_Pronoun",UserList.get(0).getNoun_Pronoun());
                       editor.putString("Status",UserList.get(0).getStatus());
                       editor.putString("Mail",UserList.get(0).getMail());
                       editor.putString("Psw",UserList.get(0).getPassword());
                       editor.commit();


                       sharedPreferencesConfigs.WriteLoginStatus(true);
                       finish();


                   }



                }

                @Override
                public void onFailure(Call<List<User>> call, Throwable t) {
                    Toast.makeText(getApplicationContext(),"failure",Toast.LENGTH_SHORT).show();
                }
            });






        }
    }

1 个答案:

答案 0 :(得分:1)

登录活动立即在此处开始主要活动:

    sharedPreferencesConfigs=new SharedPreferencesConfigs(getApplicationContext());
    if(sharedPreferencesConfigs.ReadLoginStatus()){

        startActivity(new Intent(getApplicationContext(),MainActivity.class));
        finish();

    }

为避免这种情况,请确保sharedPreferencesConfigs.ReadLoginStatus()返回false