简体   繁体   中英

Android Java : Call method of an activity in another activity

I've three activities, A,B and C.

  1. In activity A I've two functions that get sales data from firebase which I'm displaying in a recycler view.
  2. User can search for products in Activity A, the search results are shown in Activity B.
  3. Selecting a Product in Activity B opens the Activity C where user can purchase a product.
  4. At this point, I want to call the two functions of Activity A from Activity C to update the data in Activity A for the recycler View. Which leads to my original question, How do I get Activity A's method in Activity C. Or if there's a better way to do this please guide this newbie, Thanks.

update

Seems like some people misunderstood, so just wanna clarify a couple things.

  1. I cannot use fragments in this particular reason although that would be a pretty easy solution.
  2. Secondly, Everything is working fine, I was just curious if I could update the data in Activity A by adding the data in Activity C locally without needing to get it from firebase everytime. But thanks for the downvotes lol

Seems like you're overcomplicating it.

When you purchase a product in Activity C, you should store the data on Firebase database. When you go back to Activity A you should obtain the list of purchased items from firebase and display them.

You cannot call methods of Activity A from Activity C. So don't be scared of obtaining data from Firebase every single time you open the activity, in the onCreate() method.

You can make a searchList and Add those search or selected items in searchList in Activity A and share that list with Activity B via parcelable Array List in intent. From Activity A share your list by this,

ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);

and get that list in Activity B like this,

Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>)args.getSerializable("ARRAYLIST");

if you want to share that same list to Activity C then share that list again like we do in Activity A and get that list via bundle like Activity B.

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