android fragment replace 多個切換fragment時的遇到的焦點變化

 第一個fragment代碼:java

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代碼:android

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

}

  

 

切換代碼:app

getFragmentManager()
		.beginTransaction()
		.addToBackStack(null)      //入棧,默認按返回時將會返回到這個fragment。
		.replace(R.id.fragment, new FragmentSysToolsReset())  //使用的時replace函數切換fragment
		.commit();

  

遇到的問題:ide

  由於進入下一個fragment,和返回上一個fragment時。是先徹底關閉了當前顯示的fragment後再打開另外一個fragment,函數

  -->因此頁面上的焦點將會跳轉到android頁面的其餘焦點過渡。spa

  -->若,焦點上存在焦點監聽的事件也將會進行處理。blog

  

生命週期中查看,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();
    }
相關文章
相關標籤/搜索