0

I am creating an app in which in portrait mode I have an activity that has a fragment which contains a listview and on clicking on that listview, Second activity is called to show the data using second fragment but the problem is that when second fragment, i.e. Frag2 is called its onActivityCreated() method is not getting called. Why so?

MainActivity.xml

portrait mode

<LinearLayout 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="com.example.ankit.fragprac2.MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frag1"
        android:name="com.example.ankit.fragprac2.Frag1"
      />



</LinearLayout>

landscape mode

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frag1"
        android:name="com.example.ankit.fragprac2.Frag1"
        android:layout_weight="1"/>

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frag2"
        android:name="com.example.ankit.fragprac2.Frag2"
        android:layout_weight="1"/>
</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity implements Frag1.Comm {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void respond(int  i)
    {
        FragmentManager fm=getFragmentManager();
        Frag2 fg2= (Frag2) fm.findFragmentById(R.id.frag2);

        if(fg2!=null && fg2.isVisible())
        {
            fg2.setData(i);
        }
        else

        {
            Intent in=new Intent(this,SecondActivity.class);
       in.putExtra("position",i);
            startActivity(in);


        }
    }
}

Fragment1.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list"/>
</RelativeLayout>

Fragment1.java

public class Frag1 extends Fragment  {

    ListView lv;

    Comm c1;




    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_frag1,container,false);
    }

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

String[] s=getResources().getStringArray(R.array.topics);

c1 = (Comm) getActivity();
        lv = getActivity().findViewById(R.id.list);

        lv.setAdapter(new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,s));

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                c1.respond(i);

            }


        });
    }




    interface Comm
    {
        void respond(int i);
    }
}

Fragment2.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text1"
        android:layout_centerHorizontal="true"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="#000"/>
</RelativeLayout>

Fragment2.java

public class Frag2 extends Fragment {
    TextView t;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_frag2,container,false);
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Toast.makeText(getActivity(),"OAC",Toast.LENGTH_SHORT).show();
        t=getActivity().findViewById(R.id.text1);
// s1=new String[getResources().getStringArray(R.array.data).length];

//        Toast.makeText(getActivity(),""+s1.length,Toast.LENGTH_SHORT).show();
        if(savedInstanceState!=null)
        {
            String s=savedInstanceState.getString("data",null);
            t.setText(s);
        }
    }

    public void setData(int i)
    { String[] s1 =getResources().getStringArray(R.array.data);
        t.setText(s1[i]);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

        outState.putString("data",t.getText().toString());
    }
}

SecondActivity.xml

<RelativeLayout 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="com.example.ankit.fragprac2.SecondActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/frag21"
        android:name="com.example.ankit.fragprac2.Frag2"/>
</RelativeLayout>

SecondActivity.java

public class SecondActivity extends AppCompatActivity {

    int i;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

       Bundle b= getIntent().getExtras();
       if(b!=null)
        {
          i=  b.getInt("position",0);
        }


        FragmentManager fm=getFragmentManager();
     Frag2 fga2= (Frag2) fm.findFragmentById(R.id.frag21);

 fga2.setData(i);


    }
}
A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Ankit
  • 95
  • 1
  • 9
  • Possible duplicate of [onActivityResult is not being called in Fragment](https://stackoverflow.com/questions/6147884/onactivityresult-is-not-being-called-in-fragment) – vikas kumar Nov 06 '17 at 19:07

1 Answers1

0

In the SecondActivity.onCreate(), move this code:

FragmentManager fm=getFragmentManager();
Frag2 fga2= (Frag2) fm.findFragmentById(R.id.frag21);

fga2.setData(i);

To SecondActivit.onStart().

When using fga2.setData(i) in the onCreate, the Activity is not created yet; it is being created. Hence the onActivityCreated() method of the fragment has not been called yet.

Other notes:

  • Activity should extent FragmentActivity instead of AppCompatActivity.
  • you can use getSupportFragmentManager().findFragmentById to get the fragment.
  • Also see the Activity Life Cycle and Fragment LifeCycle.

Hope this helps.

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
  • Why Activity should extent FragmentActivity instead of AppCompatActivity. AppCompatActivity itself extends FragmnetActivity then if I extend AppCompatActivity then automatically FragmentActivity is extended right. – Ankit Apr 03 '18 at 03:22
  • @Ankit you are right. Some other differences also listed here: https://stackoverflow.com/questions/31297246/activity-appcompatactivity-fragmentactivity-and-actionbaractivity-when-to-us – A-Sharabiani Apr 03 '18 at 14:33