Android Java : Call method of an activity in another activity - java

I've three activities, A,B and C.
In activity A I've two functions that get sales data from firebase which I'm displaying in a recycler view.
User can search for products in Activity A, the search results are shown in Activity B.
Selecting a Product in Activity B opens the Activity C where user can purchase a product.
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.
I cannot use fragments in this particular reason although that would be a pretty easy solution.
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.

Related

Changing something in a view depending on the sequence of activities

Is it possible to change something in a view depending on the sequence of activities? For example, in an adapter I would like to highlight a text, corresponding to an element clicked in a list of elements of a previous activity. But this, only if this adapter is the result of a sequence of certain activities, given that it is used in a fragment, hosted by an activity that can in turn be called up by one of two other activities.
You can keep history of every activity opened by keeping a list/stack of ids (integer or string). First activity that starts in your app will make a new empty list and record itself into it. When another activity is opened you pass that list in the intent. Next Activity extracts the list and adds its own id in it then passes it to next Activity in intent. This way, any activity will have a history of all activities opened previously.
If you want to send additional information along with ids you can make a Parcelable class and pass that along.

Is it possible to reduce number of pages in android?

I am making an android app. this app consist of list. by clicking each item of list a new page opens.
this list contains 50 items.I should make 50 activity and their corresponding XML file.
so, is there any way that make this process easier that don't force me to make all this 50 activity one by one?
my reputation is not enough. so I upload related picture.
http://uupload.ir/files/oh1o_1.png
http://uupload.ir/files/zam9_2.png
Not sure what you mean by creating 50 pages. You have NOT to create a new class for each item in the list, instead you will create a new instance of a class. This is what programming, and OOP (Object Oriented Programming) comes into play.. Basically what you need to do is:
Create a "main_activity" class which will contain your list
Create a "item_detail_activity" which will show the details about your clicked item
What you need to write is the logic of "passing" the correct data depending on the clicked item. When an item in the "main_activity" is clicked, you will create a new instance of "item_detail_activity", passing the correct data (through a bundle).
BTW there are a lot of tutorials out there that will help you understand better the logic of an Android application.
If you have a list of items probably means that you have a list of objects which are of the same kind with same structure.
Due to this, you will have 50 pages which show to the user the same item with different values to variables, with the same structure.
You can make the objects in your list parcelable (refer to this), then pass it with the Intent to a second Activity you create, receive it, and at last populate the screen with the item you passed.
If you have troubles or doubts feel free to ask :)
EDIT:
Imagine your listview is called ListView, and the List of items you used to fill the listview is called list in the MainActivity do this:
ListView listView = (ListView) findViewById(R.Id.ListView);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// position is the index of selected item
// Launching new Activity on selecting single List Item
Intent i = new Intent(MainActivity.this, SecondActivity.class);
// sending data to new activity
i.putExtra("item", list.get(position));
startActivity(i);
}
});
in your SecondActivity, with getParcelableExtra("item") you retreieve the item clicked. Here with the variables of this item you can populate the page.
here is the doc for intents.
In secondActivity, if something must disappear if a variable has a certain value or is null, play with visibility or fragment: create an Activity with all case shown, than work with visibility or with fragments and adjust it ;)
Change your activity to fragment activity and write code to display the details corresponding listview click. it easy to follow.

Save and Restore FragmentActivities in an ArrayList

I have a list containing Activity-A, each Activity-A has different values inside it. However they all have the same class, which is Activity-A. When the user navigates back to the list from an activity, I store that activity in an ArrayList and retrieve it and start it as an intent when the user clicks on the same activity in the list again. However, that causes it to be re-created. I want it to be resumed from the state it was stored in the ArrayList. How can I do this?
Edit: I've heard of the FragmentManager and how it easily stores and can retrieve fragments. However, is there something like FragmentManager for activities? Is ActivityManager like the FragmentManager?
Edit: To clarify my question a bit more, I'm trying to:
Save the FragmentActivity to an ArrayList when it gets stopped
Resume it from the state before it was stopped.
However, when I navigate to the FragmentActivity stored in the ArrayList, the FragmentActivity is recreated from scratch, instead of resuming from its previous state.
I understand that I have to do something with saving its state. However, I doubt I can save all of its inside contents; there's lots of fragments in it.
Here's the code:
listItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showActivity(id);
}
private void showActivity(int id) {
ActivityA activityA = StaticLab.get(getActivity())
.getActivityA(id); // getActivityA simply returns the ActivityA from the ArrayList which contains the specified id
if (activityA == null) {
createNewActivityA()
}
Intent i = new Intent(getActivity(), activityA.getClass());
startActivity(i);
}
You need to crate a Static Arraylist object.
So every time you come on that activity you dont create a fresh object of ArrayList
You can use global variable but your application memory usage will be large. Where do you retrieve data? Web Service Or Client DB SQLite? If you retrieve data from SQLite, retrieve time so fast. If you retrieve from Web Service, you should save to SQLite. I don't know clear your question. just give the idea for you. Hope for Help.
Global Variable Usage

Moving String and Image between Activity Android Application

I've to develop an application that allows user to browse picture and write some information
about him, all these information will stored in a class with Person name,
After he presses NextButton the activiy should moves him to another activity with these information in the second activity he'll type another information about him,
well in the 3rd activity I should receive all these info. from ( 1st and 2nd ) activities then, showing it in the 3rd one ,,
my Questions are:
1- How can move more than one info. I write a code that moves string and other code to move picture, but I couldn't combine them with each other!
2- How can I insert the information that will be typed in the second activity to the same object of Person Class ??
Hope my Questions and my scenario is clear!!
thanks alot
Shomokh =)
-----------------------Updating----------------------------------------
// this is for String info
String FullPersonInfo = person1.toString();
Bundle basket = new Bundle();
basket.putString("key", FullPersonInfo);
Intent intent = new Intent(Form_1.this,Form_2.class);
intent.putExtras(basket);
startActivity(intent);
// I'm confusing how can I add image when i try this code it doesn't work
intent.putExtra("URI", selectedImageUri.toString() );
You can pass in extra information in the intent you are passing in to the next activity and then read the intent using intent.getExtra in the 2nd and 3rd activity. I hope that gives you better idea.
As omkar.ghaisas aluded to above you can pass extras to an Intent. However, you can only pass primitives OR any object which implements the Parcelable interface. Passing string is easy enough but the image data is trickier.
I think you have a couple of options:
Write the image path to disk and then pass the file location as a
string using the Extras in the Intent, plus any other strings you
need, e.g. other metadata
Grab the image as a byte[] array, create a class which implements
Parceleable and shuffle the byte[] around.
I think #1 is probably easier.
you can Create Parcelable object of your class that content all information whcih u want to move from one activity to another.use Bundle.putParcelable and extract all class object in another Activity.
for passing Parcelable custom or complex object between activites see:
Passing a list of objects between Activities
http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html
Android: Passing object from one activity to another
and as your application requirement but do't pass whole image in bundle just pass a reference path and text to another activity and retrieve image from path

How to open an activity twice but with different content?

I'm developing a chat app, but I have a problem.
I've a list with the contacts and when I select one contact I'm starting a new activity
Intent i = new Intent(this, MessageScreen.class);
startActivity(i);
but, when I choose another contact to talk I will use the same activity.
but it always open with the last contact screen and the variables still with the old values.
I would like to make something similar to google talk, where you can start to talk with another contact, and all the messages use the same screen, and you can change between the chats fast with no need to reconstruct the screen, reload the messages, etc..
Anyone have any idea of how to implement this?
Sliding between activities isn't a common feature, it sounds like there is one second activity that has a ViewPager that is populated with multiple chats. When starting this activity, they're probably adding the Reorder to front flag to the intent and have overridden onNewIntent to add a new view to the pager.
Try something like*:
i.PutExtra ("key", value);
before starting the activity (e.g. store the user name) and then read your value from the activity and adjust (e.g. UI) it based on the value
note: syntax could differ a bit since I do my Android stuff from C#

Categories