Moving String and Image between Activity Android Application - java

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

Related

Android Java : Call method of an activity in another activity

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.

Passing values between several activities

I've an issue with passing a variable back to the first activity.
My app starts with one activity, which opens a second one, which opens an third one, which opens a forth, which opens the first activity again.
Now I want to get a variable, which I get from a user input in the third activity in my first one. I already managed to pass variables between two activities there and back with onActivityResult() but I do not get how to manage this between more than two activities.
use bundle
you can use Bundle for move the value from first activity to second activity
check this link ---> [here] (Passing a Bundle on startActivity()?)
if use value in several activity you can use SharePrefrence or you can make
class extends Application and make value in the class and use the values in several activity
be careful if close the app destroy the values
You can use shared preferences to access variables in all your activities or use can use this method:
When going from fourth activity to first use startActivity(intent) and add the variable as an extra in intent. And in first activity override onBackPressed. This may not be good practice but it works.

Android: How can I exchange data between multiple still running activities directly?

I need a nice way to exchange data between two activities directly. I have one same custom title for all my running activities and in my first activity I display the GPS state in that title with an image (found/still searching). The LocationListener is in my first activity and if the GPS state changes, I would like to update all the titles of my running/displayed activities. At the moment I can only change the title of my first activity.
I know that I can exchange the data through the SharedPreferences and by Intents which passes the data as Extras but as far as I know, I can only receive the extras in the onCreate Methods of my other activities.
What I want do have is, that the data is updated on a still running activity (onCreate is passed).
I hope you understand what I mean :)
Example scenario:
I start up my App. The GPS localisation is running (first Activity). Meanwhile I navigate to another activity of my App. Now, If the GPS state changes I need to update the title of my second activity which is shown at the moment.
Is there any way to solve this problem?
Thanks
Apart from creating an activity with two fragments, there is a tricky way that serves for other scenarios :
Let's say, if you have an variable (let the text in the title bar is an variable) that needs to keep update, while it's impossible to use broadcast (message missing is not allowed or activity is killed), you can use the text saving from shared preference, and extract it every single time.
For example, Activity A uses String from sharepreference, key is "title", and default value is your app name. After B, C or D updates the title text, A also needs to update. So you just update the value of key "title" in share preference. After A is revoked, it will grasp the title in sharedPreference again and update the title accordingly.
Hope this help. :-)

Storing input data, then calling it back?

I'm having a problem where I just don't know how to save input user data and then recall it. Let's say I want to store someone's name, so I believe I would use an Edit Text box, but that's all I can really figure out. I then later want to display that information in another activity. If you could link me to something or guide me in the right direction it would meant the world to me.
Well if you want persistent storage even after the user closed the app, SQLite or sharedpreferences is for you. Otherwise you can juse use Bundles to pass around the small bit of data in intents when starting a new activity.
SQLite guide,Sharedpreferences guide. Hope these helped.
Use an EditText to allow the user to input their name. Get the value from the EditText component via the EditText's getText() method. After that you have lot of different ways you can go. Save it for later or just pass it on via an intent to the next activity.
Good luck.
The term "Storing" should have a scope. Say, you are gonna store the data for a specific code block, or at activity level, or at application level, or you want persistent(storing data in Preferences or Database, for example, storing the game score, etc) or non-persistent(temporary storage: declaring the variables in the activity) storage.
In your case, to get the Text from EditText and send it to next activity:
Declare a global variables,
EditText editText;
String data;
// write this inside onCreate, after the `setContentView()` method
editText = (EditText) findViewById(R.id....);
data = editText.getText().toString();
// say you want to pass the data to next activity on button click, so write this inside //onClick()
Intent intent = new Intent(currentActivityName.this, NextActivityName.class);
intent.putExtra("data", data);
startActivity(intent);
Up till now what we have done is, get text from EditText, and prepared for sending it to next it to next activity. Now, on button click, the data will be sent to next activity.
But on next activity, you need to catch the data, that was sent from previous activity, to use it.
So, in the next activity, write the following code inside onCreate()
Intent intent = getIntent();
String dataReceived = intent.getStringExtra("data");
Log.i("data received", dataReceived);
After doing this check if you got the log, with tag name "data received" and data you sent.

Java to XML Android

New to Android Development and was wondering if there was some way of taking the users input to create an activity? For example say the user is going through the process of setting up a profile of themself. One of the questions is "how many pets do you have?". The user inputs "4" and then clicks the 'Next' button (which opens the next activity).
How would I take the users input of "4" to create four editText objects in the next activity so that the user can now input the name's of his/her's pets?
I'm an ok-ish programmer (have never touched XML before though) so you don't need to go into much detail I just don't know how I would access this variable to create the four editText objects. From what I've found out you can't add strings to the resources at run-time nor can even edit/append files in resources.
I was thinking of writing an XML file from java and making the activity (written in XML) read the XML file if that's even possible? Can XML files read XML files?
this has nothing to do with xml
the user's input will be stored in a different data type like a string. If you need to load a new activity, one thing you can do is pass information in an android Bundle type.
you start new activities with intent data types. the intent data type has a putExtra method, where you can put variables in.
after you startActivity(yourIntent) the new activity can call Bundle and that has a method called getExtra associated with getIntent()
these should guide you at least
Intents.
String 1 = et.getText().toString();
String 2 = et.getText().toString();
String 3 = et.getText().toString();
String 4 = et.getText().toString();
Intent i = new Intent(this, newclass.class).
i.putExtras("key1", 1);
// and so on.
// Then you get the data by.
bundle extras = i.getExtras();
string temps1 = extras.getString("key1", null);
More info here: http://developer.android.com/reference/android/content/Intent.html
What you're asking about is not really the creation of activities, but rather the creation of layouts. As you're already aware, Android allows you to define a layout using xml and Activities have a simple method to call to set their content view based on that xml. Layouts are not required to be defined in xml though; they can be programatically created.
When you start an activity you create an Intent for it, and you are able to add "extras" to this intent; by doing that you can pass parameters into your activity and it can use those as hints for how to build the layout.
Do some research on programatic layouts (http://mylifewithandroid.blogspot.com/2007/12/xml-and-programmatic-layout.html might help) and you will probably figure out what you need to know.
You can pass data (in your case user input) with the help of intent.
You can send data by using putExtra. Here is good tutorial for you.
For dynamic layout you can inflate your current layout and add controls to your current view. For dynamic layout this is good tutorial to refer.
I hope this two two tutorial will help you.

Categories