android fragment replace 多个切换fragment时的遇到的焦点变化

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

  

posted @ 2018-12-29 09:22  liuyj_vv  阅读(3435)  评论(0编辑  收藏  举报