How to exit a fragment to an activity?

64 Views Asked by At

My app starts with 2 activity, login and registration, after login I use:

private void abrirHome() {
    Intent i = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(i);
}

After that, inside the MainAcitivity, it pulls the screens that it has to pull, in this case, the first screen that comes is HomeFragment.java, which has the xml fragment_home , and inside it has a logout button, which should return to the login screen, which is an activity.

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.HomeFragment">

    <!-- 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>


</FrameLayout>

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);
    }

}

In my last test I tried to put it inside the MainActivity, but the screen flickers and goes back to the same place.

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);
}

How do I now get out of the fragment to the activity?

private void abrirHome() {
    Intent i = new Intent(LoginActivity.this, MainActivity.class);
    startActivity(i);
}
1

There are 1 best solutions below

0
Parneet Raghuvanshi On

You may try this one...

Intent intent = new Intent(getActivity(), LoginActivity.class);
startActivity(intent);
getActivity().finish();

getActivity() returns the parent activity of the current fragment.