Does activity has cache? - android-activity

I found a problem,when I goto Activity A at first time, it will call 3 times OnLayout. And then I press back to destroy the activity,and then goto the activity again, it will call only 1 times OnLayout.Is it normal? Does activity has cache?

Related

How to implement a counter with CloudKit?

Situation
I want to manage a counter (the number of times of button is pressed) with CloudKit.
Each time a user press a button, I want to increment an integer. This integer will hence be a global (for all users) counter of how many times the button has been pressed.
Question
Is there a preferred way to do it?
My idea
I could store a CKRecord with this number and fetch and modify it each time the button is pressed. But, isn't it unsafe since it might be updated by many people (more or less) at the same time?
Your idea would work. When doing an update you will first fetch the counter record, increment the counter and then save it back. If another user has updated the counter in the mean time, then CloudKit will give you an error and you can try it again.
But it does depend on how often users will press the button. If you have multiple pushes per second during a longer period, then you will probably get a lot of errors when updating the data. The time before an update succeeds will grow.

Finish activity onPause but still be in backstack?

I'm trying to minimize memory usage in my app, and one of the things I'm doing is calling finish() in the onPause method (which I know is not the best way to do things). For the most part, it seems to be working well, but when the user clicks the back button from the next activity, it logically skips over the finished activity and goes back further. Is it possible to have that activity in the back stack and just get recreated if the user presses back?
No. This conclusion comes from the task and backstack documentation as well as the activity documentation and a general understanding of how a stack data structure works.
A stack data strucure only has 2 possible operations push/put, which adds something to the collection, and pop, which removes it. Stacks folow a last in first out model, or LIFO, where by last thing added - in your case an activity - is the first thing removed when pop is called.
Within the android lifecycle activities are generally popped from the stack when the back button is pressed. At that point onDestroy() is called and the activity is removed (you can verify this by overriding the onDestroy() method and logging the results if you want to check). Alternativly you can force onDestroy() to be called by calling finish() as you are. Finishing an activity effectivly does the same thing as pressing back. The activity is destroyed and must be recreated before it can be added to the stack.
For what you're trying to do the stack would have to incorporate some intermediate state in which an activity does not exist but rather something akin to a reference is held that, when moved to the top, would indicate that the corresponding activity should be recreated. Since this is not how the sack works - it only holds activities - that state cannont exist and so the result you are talking about is not possible.
Your Goal is to minimize memory usage,Just make use of activity life cycle, You can do this alternative(if you need)
-Just leave onCreate() method blank.(only do setContentView(layout))
-Override onResume();
-whatever you were doing in onCreate just copy paste to onResume().
-and In onPause(), Recycle your all bitmaps and set them to null(I think you are using Bitmaps thats why you are very cautious about it ). and remove your views.
Now what will happen, when you launch your new activity, onPause() would be called. that will remove your all bitmap and views. and when you come back, onResume() will be call.(onCreate will not be called). and that will again initialize your view and bitmaps.
No, i don't think that is possible. Once you finish the Activity it's gone. You could, however, implement and handle your own stack. On back pressed, you would just start the closed Activity again.

how to call previous activity in android?

I have five activity in my app,
Act1->Act2->Act3->Act4->Act5,
All activity goes in single direction as I have stated above,
Now I have to call again Act2 after successful completion of my Act5's task, so its working good and showing me the Act2 (I have Used startActivity(callIntent) in Act5 for showing me Act2 again),
After this when I click on Back button in Act2, I have Act1 (I have Used startActivity(callIntent) in Act2 for showing me Act1 again) its what I want .....
But the problem is that when I click Act1's back button it is going to put me on Act2. But I want to exit from there, because Act1 is the first initial activity.
How Can I set focus to my hidden activity , instead of creating Intent and call startActivity.
Any idea? Please help.
After long research I got pretty much understanding of "back stack" for all the activities in an app.
Each time I moved to another activity and also for opening previous activity I have used Intent.startActivity() with flag FLAG_ACTIVITY_NEW_TASK, So every time I have new activity added in the "back-stack".
I have tried intent flag as FLAG_ACTIVITY_CLEAR_TOP while calling previous activity from "back-stack" using Intent.startActivity() and I got the solution of calling back the previous activity and clears all stack top activity above the calling one activity. So now back button does working nice as I needed.
Let see My problem of calling previous activity ,
A = activity; A1->A2->A3->A4->A5->B2 , now if you click on back button you will have A1 activity , and after clicking on A1's back button you have again B2 activity which was called after A5 and so on.
After using the FLAG_ACTIVITY_CLEAR_TOP in A5 activity and called again A2 (Not creating new activity but calling precious one) I have following situation.
A = activity; A1->A2->A3->A4->A5
calling previous A2 activity, and I have following scenario.
A1->A2 only.
Thanks.
Instead of following code (with flag Intent.FLAG_ACTIVITY_NEW_TASK):
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
callIntent.setClass(CurrentActivity.this,PreviousActivity.class);
startActivity(callIntent);
Try using following one with Intent.FLAG_ACTIVITY_CLEAR_TOP
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
callIntent.setClass(CurrentActivity.this,PreviousActivity.class);
startActivity(callIntent);

How to finish an Activity that is used in a TabSpec, as the child of TabHost

I have Main Activity which contains a TabHost.
In the TabHost I have several TabSpec which use an Intent for the content of the tab.
At certain points in my code I need to remove a tab from the TabHost, and finish the Activity that was in that tab's TabSpec.
I am able to remove the tab from the TabHost, but as soon as I call finish on that child Activity, the Main Activity terminates. I have even tried calling finish some time later, with a timer. It still kills the main activity.
How can I finish a child activity without killing the main activity?
Tab Host was decrapated by google so you shouldnt use it anymore you should replace it using actionbarsherlock its a better option for tabs.

Reason for 5 sec delay to show an activity on pressing the home button?

I am facing delay of 5 sec to show an Activity after pressing the HOME key. I had gone through some post and found that after pressing the HOME button, android prevents services and broadcast-receivers from calling startActivity for 5 seconds.
Q1. Can anyone please let me know the reason to happening this delay ?
Q2. When I modified the ActivityManagerService::checkAppSwitchAllowedLocked() to always return true, it avoids checking lock and hence Activity gets shown without any delay. Would it be a good solution to avoid delay or it may cause any wrong effect in other ways ?
I know, it's probably too late (half year passed). However, here is the answer
You won't be able to get this permission on not rooted phone, unless your application is signed with the same keys as system. Android has some permissions like that.
Also, you may be interested to check this question. Starting an activity from a service after HOME button pressed without the 5 seconds delay