Keysaw
Keysaw

Reputation: 237

Access method from Dialog to MainActivity

I have a main Activity in which I created a ViewPager that instantiate 3 other Fragments. One of these is a GridView which makes a popup appear when the user click on one item. Then, in this popup, I have a simple button.

What I want to do is: when the user click on this button, I would like to access a method in my main Activity (that should change the current item of my ViewPager) and then dismiss the popup.

I tried everything I could, but I cannot achieve this... I can set up the click event on my popup and dismiss it easily, but I didn't find out how I can access a method (or even a variable) from my popup to my main Activity.

I will put my most relevant code in here so you can understand the structure of my classes (hopefully...).

My main Activity:

public class MainActivity extends FirstActivity{
private ViewPager mViewPager;

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

    mViewPager = (ViewPager) findViewById(R.id.viewpager);

    // Set an Adapter on the ViewPager
    mViewPager.setAdapter(new MainActivity_Adapter(getSupportFragmentManager()));

public void onSaveInstanceState(Bundle savedInstanceState)
{
    menuBar.onSaveInstanceState(savedInstanceState);
}}

My ViewPager activity:

public class MainActivity_Adapter extends FragmentPagerAdapter{
public MainActivity_Adapter(FragmentManager fm)
{
    super(fm);
}

@Override
public Fragment getItem(int position)
{
    // Set the color background for each page
    switch (position)
    {
        case 0:
            return MainActivity_Inventory.newInstance();
        case 1:
            return MainActivity_Map.newInstance();
        default:
            return MainActivity_AR.newInstance();
    }
}

// The number of Splash Screens to display
@Override
public int getCount()
{
    return 3;
}}

My "Inventory" Fragment

public class MainActivity_Inventory extends Fragment implements View.OnClickListener{
public static MainActivity_Inventory newInstance()
{
    MainActivity_Inventory frag = new MainActivity_Inventory();
    Bundle b = new Bundle();
    frag.setArguments(b);
    return frag;
}

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    // Select the layout
    int layout;
    layout = R.layout.activity_inventory_01;

    // Inflate the layout resource file
    View view = getActivity().getLayoutInflater().inflate(layout, container, false);

    // Set the grid view
    GridView gridview = (GridView) view.findViewById(R.id.inventory_gridView);
    gridview.setAdapter(new InventoryImageAdapter(super.getActivity()));

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        public void onItemClick(AdapterView parent, View v, int position, long id)
        {
            // Create a popup to show item details
            createPopup();
        }
    });

    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState)
{
    super.onViewCreated(view, savedInstanceState);
}

public void createPopup()
{
    DialogFragment newFragment = new PopupActivity_Inventory();
    newFragment.show(getFragmentManager(), "itemDetails");
}

@Override
public void onClick(View v)
{

}}

And my popup dialog fragment:

public class PopupActivity_Inventory extends DialogFragment{
@Override
public Dialog onCreateDialog(final Bundle savedInstanceState)
{
    // Build the alert dialog
    final Dialog dialog = new Dialog(this.getActivity());

    // Get the layout inflater
    final LayoutInflater inflater = getActivity().getLayoutInflater();

    // Set up the dialog box
    dialog.setContentView(inflater.inflate(R.layout.activity_inventory_popup_01, null));
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    dialog.getWindow().setGravity(Gravity.TOP);
    //dialog.getWindow().getAttributes().y = 100;

    (dialog.findViewById(R.id.brick_button_01)).setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            // When button is clicked, ACCESS MAIN ACTIVITY!


            dialog.dismiss();
        }
    });

    return dialog;
}}

I really hope you can help me with this... I really need to get it working. Thank you very much!

If you need further details or explanation, please just tell me.

Upvotes: 0

Views: 277

Answers (2)

xblack
xblack

Reputation: 571

The best thing to do is use EventBus library. I have a demo app in which you can add items to RecyclerView from anywhere within the app using EventBus. You can use it as a reference to simply do something else instead of current task. Here is the link to the repo:

https://github.com/code-crusher/android-demos/tree/master/EventBusDemo

And if you want to understand how it works you can refer to my article, it explains how to make communications like this easy:

https://medium.com/@code_crusher/eventbus-for-android

Hope it helps. Happy coding :)

Upvotes: 1

liminal
liminal

Reputation: 1164

Read this https://developer.android.com/training/basics/fragments/communicating.html

Look for "To allow a Fragment to communicate up to its Activity, you can define an interface in the Fragment class and implement it within the Activity...."

Another way to achieve it is using an EventBus and post events by Fragments to be caught by Activities.

Upvotes: 0

Related Questions