为什么在第二个片段中没有调用onActivityCreated()?

时间:2017-11-06 18:58:55

标签: android fragment

我正在创建一个应用程序,其中在纵向模式下我有一个活动,其中包含一个包含列表视图的片段,并且在单击该列表视图时,调用第二个活动以使用第二个片段显示数据,但问题是当第二个活动时片段,即Frag2被称为其onActivityCreated()方法未被调用。为什么这样?

MainActivity.xml

肖像模式

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

横向模式

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


    }
}

1 个答案:

答案 0 :(得分:0)

SecondActivity.onCreate()中,移动此代码:

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

fga2.setData(i);

SecondActivit.onStart()

fga2.setData(i)中使用onCreate时,尚未创建活动;它正在被创造。因此,尚未调用片段的onActivityCreated()方法。

其他说明:

  • Activity应该FragmentActivity而不是AppCompatActivity
  • 您可以使用getSupportFragmentManager().findFragmentById来获取片段。
  • 另请参阅Activity Life CycleFragment LifeCycle

希望这会有所帮助。