Start activity From Fragment Android Example

Published February 05, 2021
This post we will cover start Activity from Fragment in Android application.
We all know start one activity from other activity by startActivity();

Here we will pass the intent object with current context and target activity class name.

This is easy way to call activiyt form other activity.
Suppose we have a scenario like our Homescreen contains Navigation drawer and we added fragment inside Home Activity.

Let us say now we need to call an other activity from Fragment inside Home Activity.

So How will call the Second activity from this current fragment.

By using the below code we will call the activity from the fragment


Start Activity From Fragment Example

Step 1: Create An Android Project in Android studio
Step 2: Create Fragment with a Text button.

package com.rrtutors.activitycallfragment;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link HomeFragemnt} interface
 * to handle interaction events.
 * Use the {@link HomeFragemnt} factory method to
 * create an instance of this fragment.
 */
public class HomeFragemnt extends Fragment {

    public HomeFragemnt() {
        // Required empty public constructor
    }



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

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home_fragemnt, container, false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }


}

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".HomeFragemnt">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18sp"
        android:lineHeight="20dp"
        android:layout_margin="16dp"
        android:text="This Home Fragment. This will tell you call an activity from Fragment."
        />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_gravity="center"
        android:background="#5D0FE7"
        android:minWidth="200dp"
        android:gravity="center"
        android:padding="5dp"
        android:text="Call Activity"
        android:textColor="#FFF"
        android:textSize="18sp"
        android:layout_height="wrap_content" />

</LinearLayout>


Step 3: Add Fragment to MainActivity

To add fragment to activity in different ways
1) By Using Fragment Manager
2) By XML file fragment widget.

In this Example we will load the Fragment inside activity by XML file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

 <fragment
     android:id="@+id/fragment"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     class="com.rrtutors.activitycallfragment.HomeFragemnt"
     />
</androidx.constraintlayout.widget.ConstraintLayout>


in the above xml code we added fragment class to the fragment widget by 

class="com.rrtutors.activitycallfragment.HomeFragemnt"


step 4: Create Second Activity

Step 5: Now its time to write code for call Activity from Fragment.
Just update Fragment class by below code.

Button btn=getView().findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        Intent intent=new Intent(getActivity(),SecondActivity.class);
        startActivity(intent);
    }
});

Step 6: Run application. 

Now you can call activity from fragment by press the button.

start activity from Fragment Android


Read How to call an Activity method from Fragment

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

3645 Views