Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should startActivity always run the started Activities onCreate?

Tags:

java

android

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
like image 488
FrinkTheBrave Avatar asked Aug 10 '10 21:08

FrinkTheBrave


People also ask

What is the purpose of super onCreate () in android?

Q 9 – What is the purpose of super. onCreate() in android? The super. onCreate() will create the graphical window for subclasses and place at onCreate() method.

What happens when you call finish () inside onCreate ()?

As per official documentation: You can call finish() from within this function, in which case onDestroy() will be immediately called after onCreate(Bundle) without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

What is start activity in Android?

Starting activities or services. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.

Which method is use for start activity?

By the help of android startActivityForResult() method, we can send information from one activity to another and vice-versa. The android startActivityForResult method, requires a result from the second activity (activity to be invoked).

How does the system create an activity?

Whether an activity is the main activity that's created when the user clicks your app icon or a different activity that your app starts in response to a user action, the system creates every new instance of Activity by calling its onCreate () method.

How to start the activity 2 from the activity 1?

You can use startActivtyForResult method to start the activty2 then when onActivityResult of activty1 called call setContentView for the first one Show activity on this post.

Why does the second activity start but the first starts empty?

If you run the app and tap the button on the first activity, the second activity starts but is empty. This is because the second activity uses the empty layout provided by the template. Figure 1.

How do you start an activity from an intent?

The Intent constructor takes two parameters, a Context and a Class. The Context parameter is used first because the Activity class is a subclass of Context. The Class parameter of the app component, to which the system delivers the Intent, is, in this case, the activity to start.


1 Answers

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.

like image 155
Chris Thompson Avatar answered Sep 28 '22 06:09

Chris Thompson