Android 完全跳过的活动

Android 完全跳过的活动,android,firebase,android-intent,android-activity,firebase-realtime-database,Android,Firebase,Android Intent,Android Activity,Firebase Realtime Database,我知道这似乎是一个非常愚蠢或容易解决的问题,但老实说,我已经尽了很大努力来解决这个问题。我的应用程序应该在完成配置文件活动后显示欢迎活动,如添加您的名字和姓氏等 下面是用于设置用户配置文件的代码,因为他们填写了所需的字段,如名字、姓氏、年龄和性别 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Grab firebaseAut

我知道这似乎是一个非常愚蠢或容易解决的问题,但老实说,我已经尽了很大努力来解决这个问题。我的应用程序应该在完成配置文件活动后显示欢迎活动,如添加您的名字和姓氏等

下面是用于设置用户配置文件的代码,因为他们填写了所需的字段,如名字、姓氏、年龄和性别

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    //Grab firebaseAuth objects
    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Users");
    firebaseUser = firebaseAuth.getInstance().getCurrentUser();

    //Set app to full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_profile);

    //Get all views
    firstName = findViewById(R.id.profile_firstName);
    lastName = findViewById(R.id.profile_lastName);
    male = findViewById(R.id.profile_male);
    female = findViewById(R.id.profile_female);
    age = findViewById(R.id.profile_age);
    button = findViewById(R.id.profile_button);

    button.setOnClickListener(this);

    //If no user is logged in send them back to Login page
    if (firebaseUser == null)
    {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

}

private void createUser()
{
    String first_name = firstName.getText().toString().trim();
    String last_name = lastName.getText().toString().trim();

    String gender = "";

    if (male.isChecked())
    {
        gender = "male";
    }

    if (female.isChecked())
    {
        gender = "female";
    }

    if (male.isChecked() && female.isChecked())
    {
        Toast.makeText(this, "Please only select one gender", Toast.LENGTH_SHORT).show();
        return;
    }

    if (male.isChecked() == false && female.isChecked() == false)
    {
        Toast.makeText(this, "Please select a gender", Toast.LENGTH_SHORT).show();
        return;
    }

    int years;

    if (age.getText().toString().length() == 0)
    {
        years = 0;
    }

    else
    {
        years = Integer.parseInt(age.getText().toString());
    }



    boolean profileComplete;

    //Check if profileComplete should be true or false
    if (first_name.length() == 0 || last_name.length() == 0 || gender.length() == 0 || years == 0 || gender.length() == 0)
    {
        profileComplete = false;
    }

    else
    {
        profileComplete = true;
    }

    User user = new User(first_name, last_name, gender, years, FirebaseAuth.getInstance().getCurrentUser().getEmail(), profileComplete);

    //Create a new node for each individual user
    firebaseDatabase.getReference().child("Users/" + FirebaseAuth.getInstance().getCurrentUser().getUid()).setValue(user);

    //Bring them into the welcome screen
    Intent intent = new Intent(this, WelcomeActivity.class);
    startActivity(intent);
    finish();

}

@Override
public void onClick(View view)
{
    if (view == button)
    {
        createUser();
    }
}
在用户对象完全完成并发送到数据库后,我们可以清楚地看到该活动启动了一个名为WelcomeActivity的新活动。这有一个Log.i,从一开始就不会激发,取而代之的是ViewProfileActivity运行

以下是欢迎活动:

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    Log.i("WelcomeActivity", "We made it to the WelcomeActivity!");

    //Set app to full screen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_welcome);

    //Grab firebaseAuth objects
    String currentUserId = firebaseAuth.getInstance().getCurrentUser().getUid();

    firebaseAuth = FirebaseAuth.getInstance();
    firebaseDatabase = FirebaseDatabase.getInstance();
    databaseReference = firebaseDatabase.getReference("Users/" + currentUserId);

    welcomeUser = findViewById(R.id.id_welcome_user);

    final User user = new User();

    databaseReference.addValueEventListener(new ValueEventListener()
    {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot)
        {
            user.setFirstName(dataSnapshot.getValue(User.class).getFirstName());
            welcomeUser.setText(user.getFirstName().toString());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError)
        {

        }
    });


}
下面是ViewProfileActivity(这应该在欢迎活动之后运行。welcomeActivity现在无意运行ViewProfileActivity,因为我现在甚至无法让它首先运行到那里。):

起初

setContentView(R.layout.activity_welcome);
错误,保留layout.activity\u view\u配置文件。我原以为解决这个问题会很容易解决我的问题,但似乎什么都没有改变。我已经重建并清理了我的项目,完全卸载了apk,并在模拟器和物理设备上重新安装了它,似乎什么都不起作用。我想这是一个编码错误,但我似乎不能确定它

我的假设是其他活动正在运行,他们的onDataChange方法不断检查并执行ViewProfileActivity的意图,但我将finish()放在每个startActivity()的末尾

非常感谢所有帮助,因为这限制了我使用此应用程序的进度。到目前为止,我将继续研究,直到找到解决方案

舱单:

<?xml version="1.0" encoding="utf-8"?>

此代码位于MainActivity中,用于确定用户配置文件是否完整。如果未完成,它会将其发送到创建配置文件,否则它会立即将其发送到查看配置文件。

我认为问题在于您尚未创建要首先加载的活动

解决方案1: 尝试单击应用程序

然后选择编辑配置,并将活动设置为在指定的活动上运行

解决方案2:

.xml
文件中,按照以下方式首先启动所需的活动

  <activity
        android:name=".Activities.SplashScreen"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />      <--- add this 2 lines
            <category android:name="android.intent.category.LAUNCHER" /> <---
        </intent-filter>
    </activity>


简单地说,我创建了一个名为loggedIn的变量,该变量在开始时为false,在用户登录后立即变为true,或者跳过概要文件创建,或者不跳过概要文件创建。这将防止MainActivity将来再次调用onDataChange方法。我只是不知道还有其他的解决方案,如果有人知道另一种方法,请毫不犹豫地回答。否则,谢谢大家的支持

databaseReference.addValueEventListener(new ValueEventListener()
                {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                    {
                        if (loggedIn == false)
                        {
                            user.setProfileComplete(dataSnapshot.getValue(User.class).getProfileComplete());

                            //The profileComplete method returns TRUE, so we can skip the profile creation.
                            if(user.getProfileComplete())
                            {
                                Intent intent = new Intent(MainActivity.this, ViewProfileActivity.class);
                                startActivity(intent);
                                loggedIn = true;
                            }

                            //the profileComplete method returns FALSE, we must send the user to the profile creation.
                            else
                            {
                                Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                                startActivity(intent);
                                loggedIn = true;

                            }
                        }

                    }

您确认了
createUser
运行并到达函数末尾了吗?我用一个调试器一直跟踪它,是的,它一直执行。我刚刚运行了一个调试器,它显示它一直运行,ProfileActivity.java中的startActivity(intent)显示:“intent{cmp=com.example.aleksei.meep/.WelcomeActivity}”这意味着它指向了正确的活动。这让人费解。你能发布清单xml吗?当然,我现在会这样做,但这不只是改变第一个开始的活动吗?我可以知道流程吗?问题有点让人困惑,所以应用程序在登录屏幕上启动,但一个新用户点击进入注册,并在做出a之后Account返回登录到well..login。登录后,它会检查您是否已经通过配置文件创建屏幕。如果您没有,它会将您带到ProfileActivity,然后在创建配置文件后,它会将您带到WelcomeActivity,然后查看ProfileActivity。现在,它会完全跳过WelcomeActivity。如果用户dy的一个帐户使其跳过了ProfileActivity,因此也跳过了WelcomeActivity,从而使其进入了应用程序ViewProfileAct的核心。。
@Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                    {
                        user.setProfileComplete(dataSnapshot.getValue(User.class).getProfileComplete());

                        //The profileComplete method returns TRUE, so we can skip the profile creation.
                        if(user.getProfileComplete())
                        {
                            //Intent intent = new Intent(MainActivity.this, ViewProfileActivity.class);
                            //startActivity(intent);
                            //finish();
                        }

                        //the profileComplete method returns FALSE, we must send the user to the profile creation.
                        else
                        {
                            Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
  <activity
        android:name=".Activities.SplashScreen"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="adjustPan">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />      <--- add this 2 lines
            <category android:name="android.intent.category.LAUNCHER" /> <---
        </intent-filter>
    </activity>
databaseReference.addValueEventListener(new ValueEventListener()
                {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
                    {
                        if (loggedIn == false)
                        {
                            user.setProfileComplete(dataSnapshot.getValue(User.class).getProfileComplete());

                            //The profileComplete method returns TRUE, so we can skip the profile creation.
                            if(user.getProfileComplete())
                            {
                                Intent intent = new Intent(MainActivity.this, ViewProfileActivity.class);
                                startActivity(intent);
                                loggedIn = true;
                            }

                            //the profileComplete method returns FALSE, we must send the user to the profile creation.
                            else
                            {
                                Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                                startActivity(intent);
                                loggedIn = true;

                            }
                        }

                    }