Fragments and Activities — where do I put my application logic?

后端 未结 1 528
灰色年华
灰色年华 2020-12-07 01:48

I have created my view how I want it to look. It has 1 images, an input box, and a button. I will want to load another activity when the button is clicked. I am confused why

相关标签:
1条回答
  • 2020-12-07 02:27

    Firstly I would recommend reading this Fragments . Pay particular attention to the created fragment section, which includes the fragment life-cycle diagram. Second download and compile this Sample App,the effective navigation app will help you understand how different fragments work in tandem, and even implements a action bar.

    To answer your question more or less a fragment can be thought of as a separate class. Once you call upon that particular fragment you can call functions from within that class.

    Fragment Case-Switch

    This is some sample code to show you what I mean.

     public Fragment getItem(int i){
        switch (i) {
            case 0:
                // The first section of the app is the most interesting -- it offers
                // a launchpad into the other demonstrations in this example application.
                return new LaunchpadSectionFragment();
    
            case 1:
                return new BluetoothClass();
          
            default:
                // The GPS section of the app .
                Fragment fragment = new DummySectionFragment();
                Bundle args = new Bundle();
                args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
                fragment.setArguments(args);
                return fragment;
            }
    }
    

    In this case each fragment for me represented a class, which was implemented in a separate tab and each tab had a separate functionality. One of the key advantages of fragments is you can run separate activities without first letting one activity complete.

    Furthermore each fragment is an extension of the java.lang.Object library. So it has all those functions + additional ones. I would read this as well. Lastly it would be a good idea to have separate xml files for each fragment then you can display that separately when a fragment is invoked.

    Some more code

    Each fragment will/could have this

    public void onActivityCreated(Bundle savedInstanceState){
            super.onActivityCreated(savedInstanceState);
                // Do stuff on creation. This is usually where you add the bulk of your code. Like clickListners
    
            View rootview = inflater.inflate(R.layout.xml_the_fragment_uses container,false);
            rootview.findViewById(R.id.your_id).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Do something 
            }
        });
    }
                
        
        public void onStart(){
            super.onStart();
            Toast.makeText(getActivity(), "Fragment started",Toast.LENGTH_SHORT).show();
        }       
    
        public void onResume(){
            super.onStart();
            Toast.makeText(getActivity(), "Fragment Resumed",Toast.LENGTH_SHORT).show();
                    
        }
        
        public void onStop(){
            super.onStart();
            Toast.makeText(getActivity(), "Fragment Stoped",Toast.LENGTH_SHORT).show();
            disableBT();
        }
    

    Remember these functions are from the fragment life-cycle I mentioned earlier.

    Hopefully that gave you some idea on fragments. Also remember to read this as a lot of functionality uses the v7 app compat library. Including the fragment manager.

    0 讨论(0)
提交回复
热议问题