search

Home  >  Q&A  >  body text

android - Fragment切换的详细执行流程是怎样的?

高洛峰高洛峰2770 days ago836

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 17:01:11

    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

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 17:01:11

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:01:11

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

    reply
    0
  • Cancelreply