Я работал над клоном WhatsApp в Android Studio. после запуска на физическом устройстве я могу видеть активность входа в систему, и после того, как я даю учетные данные и нажимаю кнопку, он переключается на основную активность и выходит через 2 секунды. Почему это так?
Вот код основной деятельности
import android.content.DialogInterface;
import android.content.Intent;
import androidx.annotation.NonNull;
import com.google.android.material.tabs.TabLayout;
import androidx.viewpager.widget.ViewPager;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.appcompat.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
private Toolbar mToolbar;
private ViewPager myViewPager;
private TabLayout myTabLayout;
private TabsAccessorAdapter mTabsAccessorAdapter;
private FirebaseUser currentUser;
private FirebaseAuth mAuth;
private DatabaseReference RootRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
currentUser = mAuth.getCurrentUser();
RootRef = FirebaseDatabase.getInstance().getReference();
mToolbar = findViewById(R.id.main_page_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("WhatsApp");
myViewPager = (ViewPager) findViewById(R.id.main_tabs_pager);
mTabsAccessorAdapter = new TabsAccessorAdapter(getSupportFragmentManager());
myViewPager.setAdapter(mTabsAccessorAdapter);
myTabLayout = (TabLayout) findViewById(R.id.main_tabs);
myTabLayout.setupWithViewPager(myViewPager);
}
@Override
protected void onStart() {
super.onStart();
if (currentUser == null) {
SendUserToLoginActivity();
} else {
VerifyUserExist();
}
}
private void VerifyUserExist() {
String currentUserID = mAuth.getCurrentUser().getUid();
RootRef.child("Users").child(currentUserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if ((dataSnapshot.child("name").exists())) {
Toast.makeText(MainActivity.this, "Welcome", Toast.LENGTH_SHORT).show();
} else {
SendUserToSettingsActivity();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.options_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
if (item.getItemId() == R.id.main_logout_option) {
mAuth.signOut();
SendUserToLoginActivity();
}
if (item.getItemId() == R.id.main_create_group_option) {
RequestNewGroup();
}
if (item.getItemId() == R.id.main_settings_option) {
SendUserToSettingsActivity();
}
if (item.getItemId() == R.id.main_find_friends_option) {
SendUserToFindFriendsActivity();
}
return true;
}
private void RequestNewGroup() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,
R.style.AlertDialog);
builder.setTitle("Enter Group Name :");
final EditText groupNameField = new EditText(MainActivity.this);
groupNameField.setHint("e.g Coding Cafe");
builder.setView(groupNameField);
builder.setPositiveButton("Create", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String groupName = groupNameField.getText().toString();
if (TextUtils.isEmpty(groupName)) {
Toast.makeText(MainActivity.this, "Please write Group Name..",
Toast.LENGTH_SHORT).show();
} else {
CreateNewGroup(groupName);
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.cancel();
}
});
builder.show();
}
private void CreateNewGroup(final String groupName) {
RootRef.child("Groups").child(groupName).setValue("")
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(MainActivity.this, groupName + " is created
successfully.", Toast.LENGTH_SHORT).show();
}
}
});
}
private void SendUserToLoginActivity() {
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
}
private void SendUserToSettingsActivity() {
Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settingsIntent);
}
private void SendUserToFindFriendsActivity() {
Intent findFriendsIntent = new Intent(MainActivity.this, FindFriendsActivity.class);
startActivity(findFriendsIntent);
}
private void SendUserToChatActivity() {
Intent findFriendsIntent = new Intent(MainActivity.this, ChatActivity.class);
startActivity(findFriendsIntent);
}
}
манифест
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chatbuddy">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity" />
<activity android:name=".RegisterActivity" />
<activity
android:name=".SettingsActivity"
android:parentActivityName=".MainActivity" />
<activity android:name=".GroupChatActivity" />
<activity android:name=".PhoneLoginActivity" />
<activity
android:name="com.theartofdev.edmodo.cropper.CropImageActivity"
android:theme="@style/Base.Theme.AppCompat" />
<activity
android:name=".FindFriendsActivity"
android:parentActivityName=".MainActivity" />
<activity android:name=".ProfileActivity" />
<activity android:name=".ChatActivity"
android:parentActivityName=".MainActivity" />
</application>
</manifest>