0

我正在使用 MySQL 开发一个 Android 应用程序。我正在使用一个名为 selector.java 的类,它执行登录和注册导航。

选择器.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class Selector extends AppCompatActivity
{
    public static PreConfig preConfig;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        preConfig = new PreConfig(this);
        if(preConfig.readLoginStatus())
        {
            Intent i = new Intent(this,sample.class);
            startActivity(i);
        }
        else
        {
            Intent intentList = new 
            Intent(this,MainDrawer.class);
            startActivity(intentList);
        }
    }
}

在上述情况下,它会直接将我重定向到 MainDrawer.class,但是一旦登录成功,我需要导航到 MainDrawer.class。

如果我将上面的代码更改如下,那么它不会在成功登录后显示 MainDrawer

 if(preConfig.readLoginStatus())
            {
                Intent i = new Intent(this,sample.class);
                startActivity(i);

            }
            else
            {
                Intent i = new Intent(this,LoginActivity.class);
                startActivity(i);
            }
        }

主抽屉.java

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

public class MainDrawer extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

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


        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
              this,drawer,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        NavigationView navigationView = (NavigationView) 
        findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
    }
    @Override
    public void onBackPressed(){
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }
        else{
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.nav_drawer, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int id = item.getItemId();

        if(id == R.id.action_settings ){
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item){
        DrawerLayout drawer = (DrawerLayout) 
        findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

示例.java

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.firebase.iid.FirebaseInstanceId;

public class sample extends AppCompatActivity {

    public String printMyFCMToken()
    {
        Log.e("FCM Token=",""+ FirebaseInstanceId.getInstance().getToken());
        return FirebaseInstanceId.getInstance().getToken();
    }

    }

现在我想在成功登录后导航到 MainDrawer。我怎样才能做到这一点?

4

1 回答 1

0

1.从上面的代码没有得到什么是Sample.java 我想你已经为registrationActivity创建了它。2.假设 prefConfig 是一种 SharedPreference/db 结果,您正在检查登录状态 3.只需添加一种方法来检查用户是否已经注册。然后导航到 registrationActivity 或 login 您可以使用以下逻辑并检查它是否满足您的需求。

if(preconfig.isLoggedIn()){
    intent to MainActivity / (MainDrawer)
}else if(preconfig.isUserRegistered()){
    intent to LoginActivity()
}else{
    intent to RegistrationActivity() / (Sample)
}
于 2018-11-16T19:09:52.427 回答