Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Basic Question on Activity object

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Master Rancher
Posts: 571
9
Android Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When return to Activity A, will you pass Activity B object to A?
 
Dreke Droga
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With activityResult, yes
 
Master Rancher
Posts: 4251
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4251
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 6889
163
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 6889
163
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic