Efficient way of finishing and updating the specific previous activities - android

As I mentioned in the title, I have two questions.
1) I have user details' form in three activities.
Activities Heirarchy:
ActivityOne ---> ActivityTwo--> ActivityDetails1---> ActivityDetails2---->ActivityDetails3---> ResultActivity.
In this hierarchy, details form starts from ActivityDetails1. Submit button is in ActivityDetails3. So when I click submit button, I am submitting all the details that are entered in 3 activities to a database.If the submission is successfull, I am going to other activity(ResultActivity) by intent.
If it fails it stays on the same ActivityDetails3. Whenever submission is sucessfull, I need to finish the 3 of the ActivityDetails activities besides going to other activity. For this I am finishing these 3 activities by accessing contexts by making them static as the marked answer in this link. But this seems inefficient as the static contexts may cause memory leaks. Can some one suggest me an efficient way to do this?
2) After the submission is successful, I have to update even ActivityOne and ActivityTwo apart from going to other activity(ResultActivity) by setting the actionbar items title with one of the details submitted by the user. I am able to make the later activities update the title, but not the previous activities. Can someone please guide me how to refresh the previous activities which are in stack, so that my title of the action item on the actionbar would be changed on previous activities too.
I would be thankful, if someone can help me on my above two questions. Relevant code snippets are appreciated.

Dont type any large paragraphs.Because no one had a patient to read a large paragraph
Type your issues,problemms,what you want in brief,simple,easy to understand.
Refer the below scenarios
Activity A and Activity B.
From A go to B.If B do some operation and finishes then the result is updated in A.
In the above scenario you use following techniques
Put protected void onActivityResult(int requestCode, int resultCode, Intent data)
and handled the updation after exit of B
in A and in B before call to finish() set the result code as you required

Related

Checking the checkbox in recyclerview from a different activity

[Asking question for the first time on Stack Overflow]
I am making an application where the user will be shown with a list of locations in a recyclerView. RecyclerView has generic rows and each row has a cardview in which there is a checkbox and a textview. When the user clicks on a row, he is re-directed to a different activity where they are supposed to answer certain questions and click on save. Once clicked save, he will be re-directed to previous activity and I need that particular location's checkbox to be checked.
How can I do this?
Thanks for the help and sorry if I explained it badly.
Welcome to stack overflow. Have you read through the android documentation found here:https://developer.android.com/guide/ you are going to want to read through the activity and adapter sections of that. Also you can refer to a summarized version here https://guides.codepath.com/android/Using-an-ArrayAdapter-with-ListView#attaching-event-handlers-within-adapter
There are many ways to accomplish what you are asking. Sounds like you are looking for startActivityForResult() this method simply takes an intent and says "hey when that activity is finished I want some information back so I can do something with the result"
When you want to pass a result back to your calling activity in this case, you will need to set some data before you finish the created activity like so
Intent data = new Intent();
data.putExtra("location", selectedLocation);
setResult(RESULT_OK, data);
finish();
By doing this you can receive the result in your calling activity with the method implementation of onActivityResult(int requestCode, int resultCode, Intent data)
In your case lets say a user taps a card and you start the new activity using startActivityForResult(intent, requestCode) when the user is done with this activity and the activity is going to finish you want to set some outstate data using the Intent data passing method I showed above. Maybe you want to send the location back. Simply attach the location as an extra and receive it in your onActivityResult() implementation and then make the appropriate changes in that view based on the data. Hope that helps.
In the future, it always helps to provide example code with your question. It will help users better assist you on your issue and more importantly make users want to help you out. You seem new to android development so make sure to read through the android docs whenever you can to better your understanding of android as a whole. Cheers!

Too many Activity in the stack make the app pretty slow

Recently I created a social app. I didn't use fragment and the project is almost finished. I have several Activities like UserProfile, Followers, Followings activity.
Normally it's just working fine. But if user click UserA UserProfile activity -> and then click A's Followers -> select UserB Userprofile activity -> click B's followers activity -> select UserC Userprofile activity....
In this loop, the app would get pretty slower because it opened too many activities at same time and the back stack hold all of them.
I just wonder if there's any optimization I could do for this situation? Because UserProfile activity layout would always same except the user information content. Is that possible to use Fragment for each activity, even though different activities would show up in sequence one by one?
Thanks!
You should architect this in a different way. You should only ever have one UserProfileActivity in the stack. If you already have the UserProfileActivity for User A in the stack, and you want to show the UserProfileActivity for User B, just call startActivity() for UserProfileActivity with Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and pass some extras to indicate that the Activity should show User B. Use the same concept for all of your activities.
To make sure that the BACK button navigation works correctly, you will need to override onBackPressed() and figure out what Activity needs to be shown and with what data. Then call startActivity() and also set Intent.FLAG_ACTIVITY_REORDER_TO_FRONT and provide extras so the Activity will show the correct data.
To assist in keeping track of where you are in the navigation, you might want to create a stack of items that are stored in a static variable somewhere. Each item would indicate what Activity is being shown and with what data. Every time you launch a new Activity, you push a new item on to this stack, and every time the user presses the BACK key, you pop the top item off the stack and then look at the one underneath it to determine what Activity to start and what data to send in the extras.
With this scheme, the user can click around all day long and you will never have more than one instance of each Activity, but the user will still be able to navigate all the way back.

Return to the previous activities

In my app I have situations where I need to get a user back to some activity that preceded(not necessarilly directly) the current one. All of those previous acitivities might need Intent parameters in onCreate.
So, my question is there any easy way to get user back to an activity that might not be the direct previous activity he's been on and is it possible to avoid manual workaround of saving/restoring those previous activities' intent parameters ?
Consider an example: there's a global search-bar that can provide users with suggestions on products; once they hit one of suggested items they get moved on a product-view activity where they can reload this activity with another product - walk through. After a couple of such reloads they might decide to go back to the activity where the search was initiated, but it might not the closest to the current one.
UPD: There also should be a possibility to go back in B activities sequence.
Using startActivityForResult() while loading a new activity and using finish() to close the launched activity on back press can solve your problem.

Extras based REORDER_TO_FRONT

In an app I have very reused Activity, that shows a list of stuff happening on a specific day. The day is specified using Intent Extras.
My problem is, that if the user starts at day=1, then chooses day=2 and then day=1, from the menu, then I would like the back button to go to day=2 and then home. That is, I want to do REORDER_TO_FRONT, but not just based on the name of the activity, but also its extras.
There doesn't seam to be any intent flags suitable for this purpose. I've considered implementing my own 'sub activity stack' using onNewIntent, but it probably wouldn't work very well.
Have you tackled similar problems in your apps? Is there perhaps a way to programmatically access the activity stack, and choose which one is suitable to return to?
Manage your own Activity stack! If I'm not mistaken, you use the same Activity to display each day. Make it single top (FLAG_ACTIVITY_SINGLE_TOP). In the launching intent, pass on the current stack, in your example "121".
Respond to the back button event by launching your Activity with a smaller stack: "12" - or if stack is already "", then just let the Activity handle Back event. Then as you mentioned, use the onNewIntent function to update your Activity.

How do you skip parts of an Activity stack when returning results in Android?

I'm making an app which has a flow roughly as below:
User starts on the main screen with an empty list, hits menu, and goes to "add item." (Activity A)
User is given a new activity which allows them to specify search criteria, then hits "go" to do a search. (Activity B)
User gets a list of results, and can click on one of them to view more details. (Activity C)
User sees details of item, and can use a menu item to save it to their list in Activity A. (Activity D)
Right now, I am having each Activity call each other Activity for results, and then it is passing the result all the way back up the stack as it returns to Activity A.
Is there a way to jump this, since all I want is for a result in Activity D to get to Activity A directly?
Note that a user should still be able to navigate backwards (using the back button) through each activity, but if they explicitly save the item in Activity D, I want it to jump straight to Activity A.
I recommend just invoking the activities (not using the *ForResult) calls, then having activity D invoke Activity A with an INTENT_ADD_ITEM with data, then have Activity A add the item.
Hope this helps...
Just so that people can benefit from what I learned later...
The key to solving this problem is using flags with Intent, in this case using FLAG_ACTIVITY_CLEAR_TOP. Other flags are useful as well in controlling the flow of your UI.
It is a bad idea to try to solve this problem by chaining startActivityForResult() through activities. It means that it's difficult to change the flow of your application.

Categories

Resources