Is it possible to pass intent by intent ? - java

In my activity I add to it some stuff by checking checkbox and if
list.size()>0 shows up button which is redirecting me to second activity. In second activity I display list, when I click on it i delete object from list. Ive made button in second activity which make this list.clear(); finish(); When I return to first activity i've still visible button. How to solve it ?

you must read your 'list' at activity onResume() method: when you return to your activity, onStart() is not called since it was not killed by the system
see the entire cycle in this link: http://developer.android.com/training/basics/activity-lifecycle/stopping.html

I would use Gson to pass by intent any object of the class that has already been defined (not by you) and is not Serializable. Just for convenience, if you would like to pass an object of some class created by you, the class should extend Serializable

Related

How to change an object's fields from an activity it was not initialized in

This is my first time building a complex android app so I apologize if this is a vague question (please ask me to expand!) but I'm a little lost on how I should design/implement my application.
To make it easier, say I have a list of cars to choose from in the main activity.
Once a car is selected, it will take me to its specific settings page in another activity (the master/detail flow activity).
What I'm thinking of doing is the following:
Create a class for car with all of its fields as the various settings
Create a list of them in MainActivity
Once a car is selected, pass that specific car to the master/detail flow activity using intent
From here I'm a little confused, but I think that in the 2nd activity depending on the settings the user selects, the car in the list in the MainActivity will have its fields changed even though it was changed in another activity?
Is this the best way to approach changing settings for a list of objects initialized in the MainActivity, from another activity? I'm mostly confused on if I'm allowed to do that 4th point.
You will need to return the object to the first activity and update your list with the new data.
Start the detail activity with the startActivityForResult and then in your second activity after doing the modifications:
Intent returnIntent = new Intent();
returnIntent.putSerializableExtra("result", theObject);
setResult(Activity.RESULT_OK,returnIntent);
finish();
And then, in the first activity use the onActivityResult method to retrieve the modified object.

launching another activity what happens to arraylist in previous activity

I have an activity lets call it activity A which on creation downloads some data from a database. The data is stored in a custom ArrayList called myData.
When a user clicks on a button another activity called B is launched. Activity A passes some data to Activity B which I can do by making my class implement Parcelable.
My understanding when Activity B is lanuched that Activity A will be in either the onPause or onStop state. My question is will my arraylist myData still exist when Activity B is closed and the focus goes back to Activity A? If not how can I store this data?
As soon as you launch a new activity, the previous activity goes in onStop state.
The definition of onStop:
Called when the activity is no longer visible to the user, because
another activity has been resumed and is covering this one. This may
happen either because a new activity is being started, an existing one
is being brought in front of this one, or this one is being destroyed.
See this chart
Now when you go back to ActivityA, the array data will still be present. So when switching of activities takes place this data is not destroyed and recreated once the activityA is again started.
Yes, you can finish you ActivityB by using finish() method. And your data of arrayList will exist when you will be back to ActivityA. And i will suggest to declare arrayList as instance variable of ActivityA and fetch data on onCreate() method. so data will exist until activity destroy, so data will not fetch again when activity come to focus .

Detecting when activity changes

I am trying to add a save dialog to an Activity.
If you press back, I already have captured it and added a save dialog box.
As I have a generic action bar, is there a way to capture the change of activity event without hardcoding it into the activity change itself?
I Googled it a bit and found no luck, onDestroy and onStop don't seem to do what I want.
If you want to know whenever the Activity ceases to become visible (for whatever reason), override onPause (see http://developer.android.com/reference/android/app/Activity.html).
As an aside, as a user, I would want this behaviour - if I move away from an Activity without pressing the back button or quit, I am hoping that it will handle everything silently - ie you use onPause (or alternatives) to store things so that when the activity resumes it has everything as I left it.
One way you could do this would be write a class which inherits from Activity and implement the behavior in that class. Then simply inherit this class for all your activities.
You could write one class which implements the save behaviour
class SaveActivity extends Activity {
//...
#Override
public void onBackPressed() {
// common behaviour you want
}
}
Then in your activities you could do this
class MyActivity extends SaveActivity {
// code for this activity
}
onPause() might be what you should be looking at. It is called when any activity comes onto the top of this Activity.

Android : Text Fields are displaying empty when returning to previous activity

I have a problem with back button functionality in an Activity of Android. The first Activity having the Spinner to select one item from the Spinner's list and second one is the text field. I implemented search functionality using the Spinner and text field. The results are displaying fine as a ListView.
Here my Problem is:
While returning to the first Activity, the Spinner and text field are showing empty in the Activity. It should show the previous searched results.
Help me with the sample code/ links.
Dont create a new intent. You just need to call finish() from your second Activity to handle back event and move back to your first activity.
Its normal. When your first Activity goes to back ground its finished by System itself. So make sure to save your data in some place and in Activitie's onCreate() and onRestart() method reload the data to TextView and spinner..
Edits:
Create a Data class and Store your search results in String[] array or a String or how ever you like it. and make the class a singlton class. and when you come back to this screen fetch those data's and set Text of TextView and adapter for Spinner..
Shafi yes, i am calling the back function using Intent as Intent i=new
Intent (presentClass.this, previousClass.class);
Don't do this. Because the Activity stack will become like loop with same Activities started again n again. Instead just finish presentClass.. it will come back to previousClass
Just an addition to #ntc post look to onSaveInstanceState(Bundle outState) and onRestoreInstanceState(Bundle state) methods in Activity.

Should startActivity always run the started Activities onCreate?

I have a main activity and a sub activity.
The main activity starts the sub activity using startActivity, and passes an object in the intent.
The sub activity reads the object out of the intent in its onCreate action.
The sub activity updates the object, then returns to the main activity using startActivity, again passing the updated object back.
However, the main activities onCreate function is not called, so the code it contains to read the passed object does not run.
Further investigation indicated that the main activity onPause event is firing, i.e. it is only paused when the sub activity runs, so when the sub activity starts the main activity again, it just onResumes.
Does anyone know if there would be any disadvantages if I moved my data restore/store activities to the onResume and onPause events?
I'm not using the onCreate savedInstanceState, should I be?
How else do you pass a set of data items between Activities without using a database or those preferences? Should I be using a database? I have about 20 fairly individual data items.
Any help would be much appreciated,
Frink
I would check out the startActivityForResult() method rather than just startActivity()
That should give you a means to pass things back to the calling activity.
From http://developer.android.com/reference/android/app/Activity.html:
Sometimes you want to get a result back from an activity when it ends. For example, you may start an activity that lets the user pick a person in a list of contacts; when it ends, it returns the person that was selected. To do this, you call the startActivityForResult(Intent, int) version with a second integer parameter identifying the call. The result will come back through your onActivityResult(int, int, Intent) method.
Have a look at the Activity life cycle here.
Also, consider starting your sub-activity using StartActivityForResult.
It is more likely calling onResume() or onStart() depending on the child activity and how much control it takes.
Look at this page about 3/4's down at the graph.
http://developer.android.com/guide/topics/fundamentals.html
The parent never gets destroyed so it wouldn't have to recreate it. It should just get paused. You can override onResume() and onStart() just as you would onCreate().
Edit: Is there anything given to the user in the subactivity? It sounds like you don't actually need an activity you just need a java class that you can call methods on.
Why don't you call startActivityForResult from the main activity and get the data back in onActivityResult ? This is the normal way to do it.

Categories

Resources