Is it possible to recursively use the same activity using intents? - android

I am at a point of code where I need to use same activity with different values. Is it possible to use same activity recursively?

Yes, its possible but you have to use the condition inside the activity based on the value pass through intents.
It may possible for the same activity with different values so the value pass from previous activity will decide what data will display..

Yes.you can use the same activity but make sure,you don't go into infinite loop nor you get messed up with different codes for various conditions you call the activity.you can achieve that by maintaining extras in Intent you use to call the activity.

Wouldn't it be more suitable (in terms of readability) to create a new Activity-class with the same layout but other functionality? This way one Activity will have one behaviour instead of two - making your code more simple, readable and maintainable.

Related

android globally object reference for all activities

May be its simple question, may be its repeated question, this question s not for upvote and all.
I just want to pass my object from one activity to second and second to third activity. I know there are a lot of ways using shared preferences, Intent bundle from one to another activity.
The reason I want to know that why I can't use an object globally for all my activities and if I can how it is possible?
Thanks
There is a handful of ways in which you can achieve this.
Using a bus/listener:
https://github.com/greenrobot/EventBus
https://square.github.io/otto/
Using the application class (ready the first answer carefully, there are caveats):
How to declare global variables in Android?
And using utility classes with static methods.
Also, if you are have more than one fragment running at the same time, you can create your own interfaces and implement them where you need to receive the information.

Android: Intent vs Global

Is it better to pass a datatype like list through an intent or it's better to make it global and use everywhere?
Right now, I am not sure whether other activities in my application would be needing that list or not except the one to which i am passing the list through intent.
and making it global would break encapsulation so i am not sure what's the better way.
It completely depends on your needs.
If you just want to transfer values from one activity to another, you should go with intent. Even if there are very few number of activities which would need that value, it would be better to use Intents.
But in case you want to use the same value in all or most of your activities, you should go with global declaration.

Code Repetition Confusion

In my Android Application, I have two activities.
One Activity lets the user take a picture. This picture is saved, and then uploaded to the server. The server returns some info and displays it in a list.
The other Activity is a gallery. The user can select a picture, upload it and get the same info in a list (the same as the first activity)
The way I've implemented is this:
upload and Info task is a seperate AsyncTask called WebServiceTask. Both Activities execute this task.
I created a WebServiceTaskInvoker interface so that each activity could specify what happens on preExecute, postExecute, progressUpdate.
The problem is that the two activities pretty much do the exact same thing on preExecute, postExecute and progressUpdate so there's code repetition between the two activities.
OnPreExecute: Both Activities check internet connectivity
OnProgressUpdate: Both Activities change a TextView's text
OnPostExecute: Both Activities create a dynamic ListView and populate
it with results.
How can I fix this?
I know one way would be to combine the two activities into one but form past experience, I've known this to be troublesome and messy.
I could put the UI code in the WebServiceTask but that would lead to tight cohesion.
Implement a base class for the two activities that executes common code. Implement the activities as subclasses of your base class to execute different code.
An alternate to Catherine's suggestion is to create an activity mode enumeration.
Pass this mode as an extra when launching your activity.
If the mode is MODE_GALLERY then load the gallery.xml layout and populate it, if not then load the other layout.
Just make sure that you use the same id's for the common views, an easy way to do this is to use the include tag in your layout files.
The advantage of this is that you only have one activity file instead of three which would be required for the subclassing method.
You may also be able use fragments, but I don't have any experience with these so I can't advise ou further.
One last note, I would avoid putting UI code into a task.

How to access to Class method from another Activity in Android?

My app contains two classes: MainActivity, Activity2.
Activity2 needs to access a non-static method of MainActivity. How to do that?
I'm new with Java and Android, if you can, please explain clearly for beginners what to do.
Thanking you in advance.
Instead of calling methods from a different activity you should use Bundles to pass values from ActivityA to ActivityB when B is started from A.
Alternatively if you want to reuse code you should create a non-activity object which you can create two instances of. Say if you do a lot of heavy calculations in both activities, you can put your calculating code in a "Calculate" object. And just initiate it like you would any other Java object. Just note that these two instance will not share any data between each other.
Calculate calc = new Calculate();
calc.codeIdLikeToReuse(numbersAndStuff);
Hope this helps. I would recommend that you read up on the Activity Life Cycle to get an idea on how the life on an activity is.
Use Broad cast receiver to call methods in different activities you can find help here
and one example
Basically, you can't do that. Two activities don't communicate this way. Usually, only one activity is alive at a time (also this might not be always true). The real answer is to use Intents.
You should read some basic Android tutorial like the anddev book.
you also can use libraries like EventBus to link the code.
refer to this post

includeed layouts - update in multiple activities

I have an app with multiple activities and multiple layouts. However, one piece of layout is included on several activities. I also have a thread which updates this layout. However, when i switch activity it doesn't work. Since the layout is included the elements have the same ID's, shouldn't it just work? Or do I really need to fetch an object for each element in the layout and feed it into my thread in order to make it update the elements in a new activity?
You should run the update code for each Activity/View, although the XML included is the same, each is a different instance.
My suggestion is on Restart verify is there is any modification to do in each activity, a simple way is to each Activity extend a BaseActivity that has this code.
I include a layout for adverts in my app, but on each activity that uses it, the adverts need to be reloaded.
If I call an activity from one that is using the same included layout when I go back to the previous activity it's still there.
I guess this is what you are seeing....
So you can also save that data inside sharedPreferences (if it is little data and primitive objets or parceable objects).
Also you can extend the Application class and store the data there and update every activity inside the onResume() method. that i believe is the best way to handle this. and this is quite simple to do.
Ask google about extending the application class and he will provide tons of results on how to do it. its an easy way to pass data between activities and/or keep a reference to a single object which you will use throughout the app. Just be carefull to clear it when you wont need it anymore because it will stay in existance untill the application is finished() (which comes with the application extension living thru the whole application lifetime).

Categories

Resources