I'm working on the application with multi language support. I need to be able to change application language runtime so after updating the context with new locale I need to restart the activity. This activity draws fragments so before the restart there are few fragments in back stack.
I found recreate() method in Activity class which works fine. Problem is that this method blink the screen which looks bad.
I also found another approach how to refresh an activity. This is without a blink:
finish()
overridePendingTransition(0, 0)
startActivity(intent)
overridePendingTransition(0, 0)
Problem with this is that it removes all fragments from back stack.
Is there any other approach how to refresh the activity with fragments without a blink?
use async task
1- use dobackground method
2- use dopost method
Related
I have 2 activities with fragments inside of them. Whenever I press the back button, the fragment gets destroyed (Used FragmentTransaction's replace method for adding them into the activity) instead of going back to the first activity immediately.
How can I achieve the behavior I want to have?
You can override onBackPressed method in your activity and check if the fragmentManager.getBackStackEntryCount()==0. If yes finish() the activity.
Hope it helps.
There are two activities, Start Activity is composed VideoView and Main Activity(listview) overlays on VideoView. Because I don't know how to overlay Listview transparently on videoview at one activity. That's why I used two activity.
StartActivity(videoView) -> MainActivity(Listview)
The problem is that when I finish this app using back key, only ListView is killed. So I have to press back key again for killing videoView. I have searched all info, but I can't find out.
when you go to Listview from videoview, at that time use finish() with intent. just like below
Intent mIntent = new Intent(Activity_Listview.this,
Activity_videoview.class);
startActivity(mIntent);
finish();
What you probably want to use ("to overlay the listview") is a fragment.
Since your app will only use 1 activity it will exit immediately when the back button is pressed. This would be the correct way to do the thing you described instead of trying to kill 2 activities with 1 back-key press.
When launching MainActivity(Listview) you could use startActivityForResult (http://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent, int) ) and in the setResult method you can add some information which then to trigger the finishing of StartActivity(videoView).
Please note that these are only tricks. The correct implementation would be to use Fragments (as user3249477 suggested).
I'd like to destroy an Activity when the user clicks on a button (not the back button, a different button). I've decided to just call super.onBackPressed. Is that okay? What's a better way to close the current Activity without closing the whole app?
for closing an Activity you can use finish() , but you have to aware of back stack, if you finish your last activity in the stack so there will be no activity in your stack and you must restart your app .
see more information on : Task and BackStack
The default implementation of super.onBackPressed finishes the activity.
Instead of using super.onBackPressed , Better way to close the current activity will be to call finish() method.
Also, When you press the back button, the onResume method is called.
Use onResume Method and refresh your stuff there.
Also, if it is listView refresh the data -> https://stackoverflow.com/a/12662994/2006412
The onCreate() method is called but the new Activity never appears. No errors are logged.
Follow up - There was no problem with calling startActivty() from a Fragment (we had a bug in the second Activity that caused it to exit immediately).
startActivity() from a Fragment works exactly like startActivty() from outside a Fragment.
A fragment should not be calling startActivity(), IMHO. A fragment should be telling its activity to display something, and the activity should be deciding how to do that. In some devices, that might be launching another activity. In some devices, that might be by adding a fragment. That decision should be made at the activity level, as it is the activities that are deciding what fragments go in what activities, based upon screen size.
Here is a sample application where a click on an item in a ListFragment causes either a separate DetailsFragment to be updated (for large/xlarge screens) or pops a new activity (for normal screens).
The problem was a bug in the second Activity that caused it to exit immediately. So to answer my own question, there is no problem with calling startActivity from a Fragment. It works exactly like calling startActivity from outside Fragment.
What is the equivalent operation within an activity to navigating away from the screen. Like when you press the back button, the activity goes out of view. How can this be called from inside an activity so that it closes itself.
What about the Activity.finish() method (quoting) :
Call this when your activity is done
and should be closed.
you can use this.finish() if you want to close current activity.
this.finish()
you can use finishAffinity(); to close all the activity..
finish() method is used to finish the activity and remove it from back stack. You can call it in any method in activity. But make sure you close all the Database connections, all reference variables null to prevent any memory leaks.
You Can use just finish(); everywhere after Activity Start for clear that Activity from Stack.