Fragment的继承关系

Fragment的继承关系

Fragment的继承关系.png

Fragment的实现方式

创建一个类,继承Fragment
重写父类的方法onCreateView()
在onCreateView()方法中,为Fragment创建UI界面

Java代码
​ MyFragment类
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MyFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return getLayoutInflater().inflate(R.layout.fragment_layout, null);
}
}

​ MainActivity类
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

private MyFragment myFragment;
private MyFragment1 myFragment1;
private FragmentManager fragmentManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //创建fragment对象
    myFragment = new MyFragment();
    myFragment1 = new MyFragment1();
    //获得fragment的管理类对象
    fragmentManager = getSupportFragmentManager();
    //获得事务管理者
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //添加fragment
    fragmentTransaction.add(R.id.zhen, myFragment, "叽里咕噜");
    fragmentTransaction.add(R.id.zhen, myFragment1, "123");
    fragmentTransaction.hide(myFragment1);

// //移除fragment
// fragmentTransaction.remove(myFragment);
// //显示fragment
// fragmentTransaction.show(myFragment);
// //隐藏fragment
// fragmentTransaction.hide(myFragment);
// //替换
// fragmentTransaction.replace(R.id.zhen,myFragment);
//提交事务
fragmentTransaction.commit();
}

public void show(View view) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.show(myFragment1);
    fragmentTransaction.commit();
}

public void remove(View view) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.remove(myFragment);
    fragmentTransaction.commit();
}

public void hide(View view) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.hide(myFragment1);
    fragmentTransaction.commit();
}

public void replace(View view) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.zhen, myFragment);
    fragmentTransaction.commit();
}

}

效果图

Fragment效果图.gif

Fragment的生命周期

onAttach() Fragment与Activity关联
onCreate() 创建Fragment
onCreateView() 创建Fragment视图
onActivityCreated() 当Activity中的onCreate方法执行完后调用
onStart() 启动Fragment
onResume() Fragment可见
onPause() Fragment不可见
onStop() Fragment停止
onDestoryView() 销毁Fragment视图
onDestory() 销毁Fragment对象
onDetach() Fragment和Activity解除关联的时候调用

add生命周期执行

onAttach()
onCreate()
onCreateView()
onActivityCreated()
onStart()
onResume()

remove时生命周期执行

onPause()
onStop()
onDestoryView()
onDestory()
onDetach()

replace时生命周期执行

新Fragment --> onAttach()
新Fragment --> onCreate()
旧Fragment --> onPause()
旧Fragment --> onStop()
旧Fragment --> onDestroyView()
旧Fragment --> onDesttoy()
旧Fragment --> onDetach()
新Fragment --> onCreateView()
新Fragment --> onActivityCreated()
新Fragment --> onStart()
新Fragment --> onResume()

add时Fragment和Fctivity的生命周期执行

Activity --> onCreate()
Fragment --> onAttach()
Fragment --> onCreate()
Fragment --> onCreateView()
Fragment --> onActivityCreated()
Fragment --> onStart()
Activity --> onResume()
Fragment --> onResume()

Fragment的回退栈
Activity切换时是通过栈的形式,不断压栈出栈,在Fragment的时候,如果你不是手动开启回退栈,它是直接销毁再重建,但如果将Fragment任务添加到回退栈,情况就会不一样了,它就有了类似Activity的栈管理方式。
实现
MyFragment myFragment1 = new MyFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentTransaction = fragmentManager.beginTransaction();
//添加到回退栈
fragmentTransaction.addToBackStack(“1”);
fragmentTransaction.commit();

回退
//回退一个
fragmentManager.popBackStack();
//立刻回退一个
fragmentManager.popBackStackImmediate();

Fragment的传值
setArguments() & getArguments()
MyFragment类
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class MyFragment extends Fragment {

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = getLayoutInflater().inflate(R.layout.fragment_layout, null);
    view.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Bundle bundle = new Bundle();
            bundle.putString("key","我是fragment发送过来的数据");
            MyFragment.this.setArguments(bundle);
        }
    });
    return  view;
}

Activity类
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

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

public void show(View view) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.frameLayout, myFragment, "name");
    fragmentTransaction.commit();
}

public void receive(View view) {
    Bundle bundle = myFragment.getArguments();
    String key = bundle.getString("key");
    if (key != null) {
        Toast.makeText(this, key, Toast.LENGTH_SHORT).show();
    }
}

}

​ 效果图

Fragment_set&get传值.gif

handler传值
MyFragment类
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

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

public class MyFragment extends Fragment {
private Handler handler;
public MyFragment(Handler handler) {
this.handler = handler;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view = getLayoutInflater().inflate(R.layout.fragment_layout, null);
    view.findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Message message = Message.obtain();
            message.what = 1;
            message.obj = "我是fragment发送过来的数据";
            handler.sendMessage(message);
        }
    });
    return  view;
}

}

Activity类
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

Handler handler = new Handler(){
    @Override
    public void handleMessage(@NonNull Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 1:
                Toast.makeText(MainActivity.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
                break;
        }
    }
};
MyFragment myFragment = new MyFragment(handler);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void show(View view) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.frameLayout, myFragment, "name");
    fragmentTransaction.commit();
}

}

​ 效果图

Fragment_handler传值.gif

接口回调传值
MyFragment
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;

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

public class MyFragment extends Fragment {

private EditText input;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    //设置布局
    View view = getLayoutInflater().inflate(R.layout.fargment, null);
    input = view.findViewById(R.id.input);
    return view;
}

//接口回调
public void getEditText(Interface i){
    String msg = input.getText().toString();
    i.getInput(msg);
}

//接口
public interface Interface{
    public void getInput(String msg);
}

}

MainActivity类
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

private MyFragment fragment;
private FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fragment = new MyFragment();
    fragmentManager = getSupportFragmentManager();
}


//添加fragment
public void show(View view) {
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.framelayout,fragment,"123");
    fragmentTransaction.addToBackStack("123");
    fragmentTransaction.commit();
}


public void receive(View view) {
    //使用接口回调的方法获取数据
    fragment.getEditText(new MyFragment.Interface() {
        @Override
        public void getInput(String msg) {
            Toast.makeText(MainActivity.this, "输入了 --->"+msg, Toast.LENGTH_SHORT).show();
        }
    });
}

}

效果图

Fragment_接口回调.gif
作者:康小博链接:https://www.jianshu.com/p/0cc527924375来源:简书简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

你可能感兴趣的:(Fragment的继承关系)