android - Fragment切换的详细执行流程是怎样的?
高洛峰
高洛峰 2017-04-17 16:59:11
0
3
781
高洛峰
高洛峰

拥有18年软件开发和IT教学经验。曾任多家上市公司技术总监、架构师、项目经理、高级软件工程师等职务。 网络人气名人讲师,...

reply all(3)
左手右手慢动作

There are two strategies for fragment switching

1. Use replace to replace the current fragment
2. Use the add and hide methods to display whichever one you want to display and hide the others. In this way, compared to the first method, you don’t have to go every time when switching. Load the data once, but the memory consumption is slightly larger

fragment+viewpager

The current fragment and its adjacent fragments will be loaded to prepare for the viewpager sliding

RadioButton

Only load the current Fragment

It is recommended to choose the second one when choosing a switching strategy

伊谢尔伦

You can log your questions 1 and 2 yourself to view them, which will make you more impressed and more effective in the future.
As for the refresh function, I don’t know what kind of function you have. In fact, the last one only needs to be judged in onKeyDown. You should have no problem applying this code:

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //防止按一次退出
            if (System.currentTimeMillis() - mExitTime > 2000) {
                ToastUtils.showShort("再按一次回到桌面");
                mExitTime = System.currentTimeMillis();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
大家讲道理

Finish just ends the current activity. Let all your activities inherit the following activity to achieve the effect you want. Of course, there are other methods that you have to check on your own.

public class BaseActivity extends Activity {
    
    private long exitTime;
    
    BroadcastReceiver exitReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ExitUtils.EXIT)) {
                finish();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        IntentFilter filter = new IntentFilter();
        filter.addAction(ExitUtils.EXIT);
        this.registerReceiver(exitReceiver, filter);
        exitTime = System.currentTimeMillis();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        this.unregisterReceiver(exitReceiver);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                Intent exitIntent = new Intent();
                exitIntent.setAction(ExitUtils.EXIT);
                getApplicationContext().sendBroadcast(exitIntent);
            }
            return true;
        }

        return false;
    }
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!