简体   繁体   中英

How to cast view and invoke method inside android fragment?

I am am trying to create an application which will invoke a method inside an android fragment. Inside an Activity we generally cast view and invoke method in the onCreate() method.

Is there any way to cast view and invoke method inside android fragment? In which function call back should this method be invoked?

If you created the fragment using the New -> Fragment(Blank), and chose to create a layout file with it too. The view casting should have been done for you in the Fragment.java. If not, here is how you inflate the fragment onto the activity.

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        //fragment_example is the layout of the fragment
        return inflater.inflate(R.layout.fragment_example, container, false);
    }

To answer your question, you can definitely invoke methods inside android fragments. Where you invoke the functions depends on what you're using them for. Many of the functions can be called in

@Override
    public void onStart() {}

Again, if you create the fragment the way I pointed out, these placeholders will have been created already for you in the Fragment.java. Also remember to follow the Fragment, Presenter rule.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM