Meu app inicia com 2 activity, login e cadastro, após efetuar login eu uso:
private void abrirHome() {
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
}
Depois disso dentro da MainAcitivity ele puxa as telas que tem que puxar, no caso a primeira tela que vem é a HomeFragment.java, que tem o xml fragment_home , e dentro tem um botao deslogar, que deveria voltar para a tela de login que é uma activity.
fragment_home.xml
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:gravity="center"
android:id="@+id/drawer_layout">
<ImageView
android:id="@+id/img_login"
android:layout_width="match_parent"
android:layout_height="120dp"
android:src="@drawable/baseline_home_24" />
<TextView
android:id="@+id/txt_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Usuario Logado"
android:textColor="@color/white"
android:gravity="center"
android:layout_marginTop="15dp"
android:textSize="25sp"
android:fontFamily="casual"
/>
<Button
android:id="@+id/botao_deslogar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="@drawable/botao_cadastre_se"
android:onClick="deslogaraaaa"
android:text="Deslogar"
android:textColor="@color/white"
android:textSize="12sp" />
</LinearLayout>
HomeFragment.java
package com.example.appteste3.fragments;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.appteste3.MainActivity;
import com.example.appteste3.R;
import com.example.appteste3.activity.HomeActivity;
import com.example.appteste3.activity.LoginActivity;
import com.google.firebase.auth.FirebaseAuth;
/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {
private FirebaseAuth authetication;
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public HomeFragment() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
public void deslogar(View view){
try {
authetication.signOut();
abrirLogin();
}catch (Exception e){
e.printStackTrace();
}
}
private void abrirLogin() {
Intent i = new Intent(HomeFragment.this, LoginActivity.class);
startActivity(i);
}
}
No meu ultimo teste eu tentei colocar dentro da MainActivity, mas a tela pisca e volta pro mesmo lugar.
public void deslogar(View view){
try {
authetication.signOut();
abrirLogin();
}catch (Exception e){
e.printStackTrace();
}
}
private void abrirLogin() {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
startActivity(i);
}
Como que eu faço agora para sair do fragment para a activity?