总结:使用show 与 hide 方法切换 fragment:
fragment之间的切换不会引起fragment重走生命周期,切换时来回调用onHiddenChange方法,此方法只有在fragment之间切换调用。在activity与fragment切换时不会调用, 在onresume中操作相关事宜。
package com.example.myapplication;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import com.example.myapplication.fragment.FirstFragment;
import com.example.myapplication.fragment.ForthFragment;
import com.example.myapplication.fragment.SecondFragment;
import com.example.myapplication.fragment.ThirdFragment;
public class MainActivity1 extends AppCompatActivity implements View.OnClickListener {
Fragment firstFragment, secondFragment, thirdFragment, forthFragment;
TextView tab1,tab2,tab3, tab4;
FragmentManager fragmentManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getFragmentManager();
initView();
}
private void initView() {
tab1=findViewById(R.id.tab1);
tab2=findViewById(R.id.tab2);
tab3=findViewById(R.id.tab3);
tab4=findViewById(R.id.tab4);
tab1.setOnClickListener(this);
tab2.setOnClickListener(this);
tab3.setOnClickListener(this);
tab4.setOnClickListener(this);
}
void showFragment(int id) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
hideFragment(fragmentTransaction);
switch (id) {
case R.id.tab1:
if (firstFragment == null) {
firstFragment = new FirstFragment();
fragmentTransaction.add(R.id.content, firstFragment);
}
fragmentTransaction.show(firstFragment);
break;
case R.id.tab2:
if (secondFragment == null) {
secondFragment = new SecondFragment();
fragmentTransaction.add(R.id.content, secondFragment);
}
fragmentTransaction.show(secondFragment);
break;
case R.id.tab3:
if (thirdFragment == null) {
thirdFragment = new ThirdFragment();
fragmentTransaction.add(R.id.content, thirdFragment);
}
fragmentTransaction.show(thirdFragment);
break;
case R.id.tab4:
if (forthFragment == null) {
forthFragment = new ForthFragment();
fragmentTransaction.add(R.id.content, forthFragment);
}
fragmentTransaction.show(forthFragment);
break;
}
fragmentTransaction.commit();
}
void hideFragment(FragmentTransaction fragmentTransaction) {
if (firstFragment != null) {
fragmentTransaction.hide(firstFragment);
}
if (secondFragment != null) {
fragmentTransaction.hide(secondFragment);
}
if (thirdFragment != null) {
fragmentTransaction.hide(thirdFragment);
}
if (forthFragment != null) {
fragmentTransaction.hide(forthFragment);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
serverManager.Stop();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tab1:
case R.id.tab2:
case R.id.tab3:
case R.id.tab4:
showFragment(view.getId());
break;
}
}
}
BaseFragment
package com.example.myapplication.fragment;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BaseFragment extends Fragment {
String Tag="Test---";
@Override
public void onStart() {
super.onStart();
Log.i(Tag+"onStart()", this.getClass().getName());
}
@Override
public void onResume() {
super.onResume();
Log.i(Tag+"onResume()", this.getClass().getName());
}
@Override
public void onPause() {
super.onPause();
Log.i(Tag+"onPause()", this.getClass().getName());
}
@Override
public void onStop() {
super.onStop();
Log.i(Tag+"onStop()", this.getClass().getName());
}
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
Log.i(Tag+"onHiddenChanged()" + hidden, this.getClass().getName());
}
}
举例一个FristFragment
public class FirstFragment extends BaseFragment{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.first_fragment,null);
return view;
}
}
xml 文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first fragment"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</android.support.constraint.ConstraintLayout>
首页xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.myapplication.MainActivity">
<View
android:id="@+id/hline1"
android:layout_width="match_parent"
android:layout_height="1dp"
app:layout_constraintBottom_toTopOf="@+id/tab1"
android:background="#535353"
/>
<TextView
android:id="@+id/tab1"
android:layout_width="0dp"
android:layout_height="40dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/tab2"
android:text="1"
android:gravity="center"
/>
<View
android:id="@+id/view1"
android:layout_width="1dp"
android:layout_height="40dp"
android:background="#535353"
app:layout_constraintLeft_toRightOf="@+id/tab1"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/tab2"
android:layout_width="0dp"
android:layout_height="40dp"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tab1"
app:layout_constraintRight_toLeftOf="@+id/tab3"
android:text="2"
android:gravity="center"
/>
<View
android:id="@+id/view2"
android:layout_width="1dp"
android:layout_height="40dp"
android:background="#535353"
app:layout_constraintLeft_toRightOf="@+id/tab2"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/tab3"
android:layout_width="0dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tab2"
app:layout_constraintRight_toLeftOf="@+id/tab4"
app:layout_constraintHorizontal_weight="1"
android:text="3"
android:gravity="center"
/>
<View
android:id="@+id/view3"
android:layout_width="1dp"
android:layout_height="40dp"
android:background="#535353"
app:layout_constraintLeft_toRightOf="@+id/tab3"
app:layout_constraintBottom_toBottomOf="parent"
/>
<TextView
android:id="@+id/tab4"
android:layout_width="0dp"
android:layout_height="40dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@+id/tab3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_weight="1"
android:text="4"
android:gravity="center"
/>
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/hline1"
app:layout_constraintTop_toTopOf="parent"
>
</FrameLayout>
</android.support.constraint.ConstraintLayout>