java.lang.IllegalStateException: Activity has been destroyed at Calling fragment inside another fragment to replace on framelayout

eureka

I am new to android and learning fragment instead of multiple, i have problem. when i call planet fragment from friends "java.lang.IllegalStateException: Activity has been destroyed" android runtime FATAL exception occurs here is my code. i saw this page but couldn't figure out how to use it. I will be grateful for any help

fragment_planet.xml

        <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:gravity="center"
    android:padding="32dp" />

Friends.java

    package com.app.hubara;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Friends extends Fragment {

    public Friends() { }

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.content_friends, container, false);

        MainActivity main = new MainActivity();
        main.friends();

        return rootView;
    }


}

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="100dp"/>

    <Button
        android:id="@+id/display"
        android:height="70dp"
        android:width="300dp"
        android:layout_marginTop="0dp"
        android:text="Display"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

MainActivity

    package com.app.hubara;

import android.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;

import java.util.Locale;

import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = (Button) findViewById(R.id.display);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Friends frag_friends = new Friends();
                fragmentManager = getFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.content_frame, frag_friends);
                fragmentTransaction.commit();
            }
        });

    }

    public void friends(){
        PlanetFragment planetFragment = new PlanetFragment();
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, planetFragment);
        fragmentTransaction.commit();
    }


    public static class PlanetFragment extends Fragment {

        public PlanetFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
            int i = getArguments().getInt("Earth");
            String planet = getResources().getStringArray(R.array.planets_array)[i];
            int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
                    "drawable", "Earth");
            ((ImageView) rootView.findViewById(R.id.image)).setImageResource(imageId);
            getActivity().setTitle(planet);
            return rootView;
        }

    }


}
hata

To access an Activity, you shouldn't do this:

MainActivity main = new MainActivity();
main.friends();

Because this destroys current Activity then instantiates another new Activity.

You can access it appropriately by calling getActivity():

MainActivity main = getActivity();
main.friends();

Note that actually you are recommended to define and use an interface to communicate between Fragment and Activity. If you want to learn further, please refer to the official training.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

java.lang.IllegalStateException: FragmentManager has been destroyed

From Java

java.lang.IllegalStateException: Fragment not attached to Activity

From Dev

java.lang.IllegalStateException: Fragment already added

From Dev

Calling Activity Method From Inside A Fragment

From Dev

"java.lang.IllegalStateException: Activity has been destroyed" when press onBackPressed()

From Dev

Calling DialogFragment from Activity resulting in "IllegalStateException: fragment not attached to Activity"

From Dev

java.lang.IllegalStateException: Activity has been destroyed

From Dev

nested fragment java.lang.IllegalStateException: Activity has been destroyed

From Dev

Stop handler after the fragment has been destroyed

From Dev

"Activity has been destroyed" error on Fragment testing with Robolectric

From Dev

IllegalStateException : Activity has been destroyed

From Dev

ChildFragmentManager java.lang.IllegalStateException: Activity has been destroyed

From Dev

java.lang.IllegalStateException: FragmentManager has been destroyed

From Dev

ChildFragmentManager java.lang.IllegalStateException: Activity has been destroyed

From Dev

Replacing FrameLayout in Activity by a Fragment does not replace the whole FrameLayout

From Dev

Android link a fragment activity to a framelayout

From Dev

Android replace a fragment with an activity into a framelayout

From Dev

Activity has been destroyed while adding a Fragment to a ViewPager

From Dev

Android nested fragments - java.lang.IllegalStateException: Activity has been destroyed

From Dev

Handling "Activity has been destroyed" (java.lang.IllegalStateException) exception?

From Dev

Navigate back to calling Fragment from another activity

From Dev

IllegalStateException : Activity has been destroyed

From Dev

How to select with espresso : an item in an adapter, present in a fragment inside a framelayout of an activity?

From Dev

Trying to replace main_activity fragment with another fragment onbuttonclick

From Dev

Android: Cant replace fragment inside activity with another fragment?

From Dev

Map Fragment inside another fragment causes Activity to crash

From Dev

java.lang.IllegalStateException: Fragment already active

From Dev

How to replace FrameLayout in Activity with a fragment?

From Dev

IllegalStateException: Fragment has not been attached yet when using BottomNavigation

Related Related

  1. 1

    java.lang.IllegalStateException: FragmentManager has been destroyed

  2. 2

    java.lang.IllegalStateException: Fragment not attached to Activity

  3. 3

    java.lang.IllegalStateException: Fragment already added

  4. 4

    Calling Activity Method From Inside A Fragment

  5. 5

    "java.lang.IllegalStateException: Activity has been destroyed" when press onBackPressed()

  6. 6

    Calling DialogFragment from Activity resulting in "IllegalStateException: fragment not attached to Activity"

  7. 7

    java.lang.IllegalStateException: Activity has been destroyed

  8. 8

    nested fragment java.lang.IllegalStateException: Activity has been destroyed

  9. 9

    Stop handler after the fragment has been destroyed

  10. 10

    "Activity has been destroyed" error on Fragment testing with Robolectric

  11. 11

    IllegalStateException : Activity has been destroyed

  12. 12

    ChildFragmentManager java.lang.IllegalStateException: Activity has been destroyed

  13. 13

    java.lang.IllegalStateException: FragmentManager has been destroyed

  14. 14

    ChildFragmentManager java.lang.IllegalStateException: Activity has been destroyed

  15. 15

    Replacing FrameLayout in Activity by a Fragment does not replace the whole FrameLayout

  16. 16

    Android link a fragment activity to a framelayout

  17. 17

    Android replace a fragment with an activity into a framelayout

  18. 18

    Activity has been destroyed while adding a Fragment to a ViewPager

  19. 19

    Android nested fragments - java.lang.IllegalStateException: Activity has been destroyed

  20. 20

    Handling "Activity has been destroyed" (java.lang.IllegalStateException) exception?

  21. 21

    Navigate back to calling Fragment from another activity

  22. 22

    IllegalStateException : Activity has been destroyed

  23. 23

    How to select with espresso : an item in an adapter, present in a fragment inside a framelayout of an activity?

  24. 24

    Trying to replace main_activity fragment with another fragment onbuttonclick

  25. 25

    Android: Cant replace fragment inside activity with another fragment?

  26. 26

    Map Fragment inside another fragment causes Activity to crash

  27. 27

    java.lang.IllegalStateException: Fragment already active

  28. 28

    How to replace FrameLayout in Activity with a fragment?

  29. 29

    IllegalStateException: Fragment has not been attached yet when using BottomNavigation

HotTag

Archive