• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Basic Question on Activity object

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

     I'm new to Android. I have basic question. Consider two Activity A, Activity B.

     Activity A start Activity B and returns back to Activity A After Activity B process complete. Now image Activity B gets called once and it will have Activity B object created. once that is done, it will return back to Activity A. And if Activity B again gets called from Activity A, will it be same Activity B object that got called previously or a new object with new reference will be created for Activity B called second time?
 
Rancher
Posts: 520
7
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When return to Activity A, will you pass Activity B object to A?
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With activityResult, yes
 
Rancher
Posts: 3965
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the code for Activity B that shows how it passes a reference to itself back to Activity A?
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not passing the Activity B reference. Below is the method signature.

public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 switch(requestCode) {
     if (resultCode == Activity.RESULT_OK) {
       String returnValue = data.getStringExtra("ab");
 
Norm Radder
Rancher
Posts: 3965
36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

will it be same Activity B object that got called previously or a new object


Why do you need to know that?  I imagine the old object is collected and a new one created as needed.
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Norm Radder wrote:

will it be same Activity B object that got called previously or a new object


Why do you need to know that?  I imagine the old object is collected and a new one created as needed.



Well, I may not be able to give you why I need to know, but I need to concretely understand how the objects are behaving. Any reference to article or documentation in this subject area in order to understand will be greatly helpful.

Thank you,
Dreke
 
Saloon Keeper
Posts: 6704
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It will not be the same Activity object, because Android does not know beforehand which activity will be called as the result of a startActivity call (that can change dynamically at runtime). This is easy to test by comparing the references of the different activity objects.

There are other circumstances under which an existing Activity object might get reused; the Activity Lifecycle documentation talks about that.
 
Dreke Droga
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tim Moores wrote:It will not be the same Activity object, because Android does not know beforehand which activity will be called as the result of a startActivity call (that can change dynamically at runtime). This is easy to test by comparing the references of the different activity objects.

There are other circumstances under which an existing Activity object might get reused; the Activity Lifecycle documentation talks about that.



Thank you. I will print the references and verify. You said "Activity object might get reused". Can you please give me an example?

 
Tim Moores
Saloon Keeper
Posts: 6704
161
Android Mac OS X Firefox Browser VI Editor Tomcat Server Safari
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See figure 1 in https://developer.android.com/guide/components/activities/activity-lifecycle. If the activity's onStop method hasn't been called yet and the user navigates back to it, then the existing Activity object will be reused.
 
Grow your own food... or this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic