简体   繁体   中英

What is the correct approach / logic to access the activity which inflates a layout with custom view from this view?

I'm developing an application in which I have a dashboard with 4 buttons. Each button starts a new Intent to a different Activity. Each Activity inflates the same layout that consists of the custom view. In the custom view is a ViewPager. In the ViewPager I want to display a strings that resides in the arrays in the mentioned above activities. Should I pass those arrays to the custom view and display it in the ViewPager? If so how can I pass them? How the Custom View would know from which activity they come from? I know that the 'sender' activity will be the one that is currently running but how can I check it? Or should I just make the arrays static and easily access them? In this case I would also need to know which activity to access. Please advise or maybe there is a better way of implementing it. Thank you.

Intents let you start other activities , also allows you to pass primitives, primitive arrays/Lists as well as custom Parcelable objects to other activities.

You can pass array data, starter activity name etc here. Or you can use getCallingActivity() to know the sender activity.

UPDATE:

You can pass data to custom views in onCreate() , if You have setter methods in Custom View class.

For more complex communication, Interface your activity to its child view as shown:

Interface:

public interface CustomViewParent {
    //----add as many communication methods you want---
    public String[] getData();
    public void doSomething();
}

Activity:

public class MyActivity extends Activity implements CustomViewParent {

// ------------------------ INTERFACE METHODS ------------------------


// --------------------- Interface CustomViewParent ---------------------

    @Override
    public String[] getData() {
        return new String[]{"data1","data2"};
    }

    @Override
    public void doSomething() {
        Toast.makeText(this,"Custom view called me !",Toast.LENGTH_SHORT).show();
    }

// -------------------------- OTHER METHODS --------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        //----here we set the parent--
        ((CustomView)findViewById(R.id.my_custom_view)).setParent(this);
    }
}

Accept and use this interface in Custom View:

public class CustomView extends ViewPager {
// ------------------------------ FIELDS ------------------------------

    private CustomViewParent parent;

// --------------------------- CONSTRUCTORS ---------------------------

    public CustomView(Context context) {
        super(context);
        init();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

// --------------------- GETTER / SETTER METHODS ---------------------

    public void setParent(CustomViewParent parent) {
        this.parent = parent;
    }

// ---------------------------- INITIALIZE-----------------------------
    private void init() {
    //-----initialize/inflate custom views-----

    //-----calls to parent, !! do not forget the null check !! ----
        if(parent != null){
            String[] data = parent.getData();

            parent.doSomething();
        }

    //----add data from parent to Views etc---

    }
}

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