如果在加载Activity时已添加一个片段,则替换片段将不会在先前加载的片段暂停时调用

时间:2014-08-24 19:28:49

标签: android

There is one activity MainActivity.
there are two fragmnet 
FragmentOne
FragmentTwo

Q.1 - initially first fragment loaded with activity.
and all the method related to this fragment called(like onAttach, oncreate, onCreateview, onViewCreated, onActivityCreated).
but when i click on button2(see above xml). it will not call on pause of the already loaded fragment, it will again start 
new fragment with start(onAttach, oncreate, onCreateview, onViewCreated, onActivityCreated). so how this work?
Q.2 - IS it necessary to always 
define android:name="com.example.fragexample.FragmentOne" class name with fragment when making defining fragment tab?


This is my main Activity layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
  <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Fragment No.1"
        android:onClick="selectFrag" />
     <Button
         android:id="@+id/button2"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:onClick="selectFrag"
         android:text="Fragment No.2" />
     <Button
         android:id="@+id/btn_go_to_next_activity"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:text="Go To Home Activity"
         android:onClick="goToNextActivity"
          />
   <fragment
       android:name="com.example.fragexample.FragmentOne"
        android:id="@+id/fragment_place"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>



Fragment One Code
package com.example.fragexample;

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

public class FragmentOne extends Fragment {

    public static String FragmentOneTag = "FragmentOne";
    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {

        //Inflate the layout for this fragment
        //Toast.makeText(getActivity(), "FragmentOne  - "+container+"  bundle -- "+savedInstanceState, Toast.LENGTH_LONG).show();
        Log.i(FragmentOneTag, "onCreateView: "+"inflater  -- "+inflater+"  container  -- "+container+"  bundle -- "+savedInstanceState);
        //Log.i(FragmentOneTag, "inflater  -- "+inflater+"  container  -- "+container+"  bundle -- "+savedInstanceState);
        return inflater.inflate(R.layout.fragment_one, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i(FragmentOneTag, "onAttach"+" Activity:"+activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(FragmentOneTag, "onCreate: savedInstanceState:"+savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(FragmentOneTag, "onActivityCreated"+" savedInstanceState"+savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(FragmentOneTag, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(FragmentOneTag, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(FragmentOneTag, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(FragmentOneTag, "onStop");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(FragmentOneTag, "onDestroy");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(FragmentOneTag, "onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(FragmentOneTag, "onDetach");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Log.i(FragmentOneTag, "onViewCreated "+"view:"+view+" savedInstanceState:"+savedInstanceState);
    }

}


Fragment Two Code

package com.example.fragexample;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentTwo extends Fragment{
    private static String FragmentTWOTag = "FragmentTwo";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
            Log.i(FragmentTWOTag, "onCreateView");
                 //     Toast.makeText(getActivity(),"inflater  -- "+inflater+"         FragmentTWo  - "+container+"  bundle -- "+savedInstanceState, Toast.LENGTH_LONG).show();
        Log.i("FragmentTwo", "inflater  -- "+inflater+"  container  -- "+container+"  bundle -- "+savedInstanceState);
        return inflater.inflate(
                R.layout.fragment_two, container, false);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        Log.i(FragmentTWOTag, "onAttach");
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i(FragmentTWOTag, "onCreate");
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Log.i(FragmentTWOTag, "onActivityCreated");
    }

    @Override
    public void onStart() {
        super.onStart();
        Log.i(FragmentTWOTag, "onStart");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(FragmentTWOTag, "onResume");
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i(FragmentTWOTag, "onPause");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.i(FragmentTWOTag, "onStop");
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(FragmentTWOTag, "onDestroy");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i(FragmentTWOTag, "onDestroyView");
    }

    @Override
    public void onDetach() {
        super.onDetach();
        Log.i(FragmentTWOTag, "onDetach");
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) 
    {
        super.onViewCreated(view, savedInstanceState);
        Log.i(FragmentTWOTag, "onViewCreated");
    }
}

我正在显示带有第一个片段的初始屏幕。 第一个屏幕有两个选项按钮 片段No.1和片段2

点击Fragment No.1将加载第一个片段 点击Fragment No.2将加载第二个片段 请指导我的工作方式。 在此先感谢。

0 个答案:

没有答案