using onBackPressed in android - android

I am working on an android application project which has more than 8 activities just up to now. I need to learn how back button behave in android. Because I need to override it and do some actions when it pressed. When back button pressed android looks up the trace file and go to the activity which you came from to the current activity. However in my application you can go to an activity from several other activities. And I should know from where i came to this activity so that i can decide whether I should override onBackPressedmethod or not. But I don't want to do this with carrying some parameters with something like putExtra and startActivity. Is there a better way to handle this problem? Thanks

Sure override onbackpressed and use intent to traverse to what ever class you want to move.

You could try doing a BaseActivity and putting your custom logic there.
All of your 8 activities would extend that BaseActivity that has custom behavior there.
From that BaseActivity you can do whatever logic you want in onBackPressed based on a custom variable that you would send.
I wouldn't recommend overriding onBackPressed ever. Check the other lifecycle methods: onPause or onDestroy.
If you want to do something like a custom navigation you should check the ActionBar goUp instead.

You can find out which activity launch this activity by calling getCallingActivity ()

Related

Can I call interface from one activity to another activity?

I am just asking for knowledge , can we call Interface from one activity to another activity
If it possible then can anyone share code with me?
You can't
Why?
Activity object is created by system. We all just call startActivity() & work in its lifecycle methods like onCreate(), onStart() etc.
If you really need it you can use fragment.

how to destroy all the activites except the latest 3?

I have activities like A->B->C->D. How can I close the A activity if I have 4 activities on my stack? Also later when I open activity E i want B to be closed aswell, so I want to have C->D->E only.
There is no such as a direct way to manage activities number in stack. So far I know that stack is big as much as available memory.
Also consider LaunchMode and whether activities are in the same task or not.
So, you might implement your own Activity manager to finish un-wanted activities.
Here is briefly how I see the solution:
Create a model to store activity, its index in stack, date ..i.e. ActivityItem
Create an empty List of ActivityItem in your custom Application. To avoid memory leak, use WeakReference. create a public setter adActivity to add and manage activities
Better approach to create activity base class and reuse it as superClass wherever you want to manage count of running activities. instead of repeating same implementation for each different activity.
OnCreate in your base Activity call adActivity and pass current activity.
adActivity job is firstly clean the list of destroyed activities. thanks WeakReference. Then manually kill older activities before last 3 items in your list. It's not easy as it looks.. for example: SingleInstance and rotation will make it challenge to achieve this :-)
that is it.
Good luck,'.
When you start activity use
startActivityForResult(intent,code);//different code for all activity
and call
finishActivity(code);//which activity do you want to close?
You can call:
finish();
after:
startActivity(intent);
In every activity that you want to close, when you open another activity.

Android: how to navigate between various activities without losing state?

Right now my activities behave as below:
But of course I'd like to maintain state as I navigate between activities so ideally it should behave like this:
What is the correct Android way to do that?
You can save the state by overriding the onPause() method of activity and reset it in the onResume() method. You can also use SharedPreferences to share state between activities. See SavingPersistentState.
Additionaly you can also go through the Activity Cycle.

android activity backstack management

My app shows some content (video,pdf,img, etc ) and within every content I can start another content. What I want is to have only "one back history".
For example, if my activity history is like this:
VideoActivityIns1->PdfActivityIns1->VideoActivityIns2
I need to go back from VideoActivityIns2 to PdfActivityIns1, but one step back is should be MainActivity of my app.
How can I do this? Any help would be appreciated
Each activity has activity lifecycle methods you can override to achieve the result you need. Thus, you can either launch Activity2 onResume() on Activity1 onPause(),
http://developer.android.com/training/basics/activity-lifecycle/index.html
or, invoke ActivityManager to detect and manage the other activities.
http://developer.android.com/reference/android/app/ActivityManager.html
You can also make use of intent resolution mechanism to assign several priorities to your activities and then setup intent filters in each activity so you can start activities with a given priority in your code. You can do this either in Java or XML (though I suggest Java). Have a look at the Intent class.

How to make a core of activity on android?

I am new to Android development. After learning from many tutorials I got many Activities and many Fragments. How can I make a core engine to check what Activity is running and what Fragment is showing on a container?
Assume that I have:
Acivity01, Activity02, ... , Activity10
Fragment01, Fragment02, ... , Fragment10
I want to make a class that filters the Activity where Activity is on runtime and what Fragment is embeded to that activity.
How can I do this?
If I understand you correctly, you may want to store some references within your Application class to an Activity and to Fragment instance(-s), which are currently in foreground (by this I mean that user can instantly interact with Activity/Fragment).
As for Activity
Create some Activity field in your Application class and getter/setter methods for it (e.g., setCurrentActivity(), getCurrentActivity()). Then call setCurrentActivity() from onResume() method for each of your Activity instances. Don't forget to call setCurrentActivity, supplying null reference to ir in order to properly handle a case, when there are no foreground activities, but application is stll working.
As for Fragment
The general idea is similar to the first item, but there can be more than one Fragment instance in foreground state at time. So you need to store something like List, where you add your resumed fragments and remove paused.
You may also want to implement something similar for dialogs, for example. Then use the same strategy. Hope it will help.

Categories

Resources