第一個fragment代碼:
package com.example.liuyj.mstarsysseting.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.example.liuyj.mstarsysseting.R;
public class FragmentSysTools extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_systools, container, false);
view.findViewById(R.id.toolsReset).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.addToBackStack(null)
.replace(R.id.fragment, new FragmentSysToolsReset())
.commit();
}
});
view.findViewById(R.id.toolsReset).requestFocus();
return view;
}
@Override
public void onStart() {
Log.e("FragmentSysTools", "onStart");
super.onStart();
}
@Override
public void onResume() {
Log.e("FragmentSysTools", "onResume");
super.onResume();
}
@Override
public void onPause() {
Log.e("FragmentSysTools", "onPause");
super.onPause();
}
@Override
public void onStop() {
Log.e("FragmentSysTools", "onStop");
super.onStop();
}
@Override
public void onDestroy() {
Log.e("FragmentSysTools", "onDestroy");
super.onDestroy();
}
}
第二個fragment代碼:
package com.example.liuyj.mstarsysseting.fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import com.example.liuyj.mstarsysseting.R;
import java.security.Key;
public class FragmentSysToolsReset extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_systools_reset, container, false);
Button button = view.findViewById(R.id.toolsRest_test);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().popBackStack();
}
});
button.requestFocus();
return view;
}
@Override
public void onStart() {
Log.e("FragmentSysToolsReset", "onStart");
super.onStart();
}
@Override
public void onResume() {
Log.e("FragmentSysToolsReset", "onResume");
super.onResume();
}
@Override
public void onPause() {
Log.e("FragmentSysToolsReset", "onPause");
super.onPause();
}
@Override
public void onStop() {
Log.e("FragmentSysToolsReset", "onStop");
super.onStop();
}
@Override
public void onDestroy() {
Log.e("FragmentSysTools", "onDestroy");
super.onDestroy();
}
}
切換代碼:
getFragmentManager() .beginTransaction() .addToBackStack(null) //入棧,默認按返回時將會返回到這個fragment。 .replace(R.id.fragment, new FragmentSysToolsReset()) //使用的時replace函數切換fragment .commit();
遇到的問題:
因為進入下一個fragment,和返回上一個fragment時。是先完全關閉了當前顯示的fragment后再打開另一個fragment,
-->所以頁面上的焦點將會跳轉到android頁面的其他焦點過渡。
-->若,焦點上存在焦點監聽的事件也將會進行處理。
生命周期中查看,fragment的運行狀態:

解決辦法:使用 add() hide() show() 函數來處理: 切換時 hide() 函數放在 show() 函數后面
public void showFragment(Fragment fragmentTo) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
if(fragmentCurr == fragmentTo) {
return;
}
if (fragmentTo.isAdded()) {
transaction.show(fragmentTo);
} else {
transaction.add(R.id.fragment, fragmentTo).show(fragmentTo);
}
if (null != fragmentCurr) {
transaction.hide(fragmentCurr); //hide()函數放在show()函數后面。保證焦點存在
}
fragmentCurr = fragmentTo;
transaction.commit();
}
