As ActivityGroup manages the Activities in the form of view, so when I try to return back to the Parent Activity that called the child Activity in ActivityGroup the onResume() is not being called.
I tried calling the OnResume() like this.
((Activity)view.getContext()).onResume();
But its not working, instead finish() is working is fine for me.
((Activity)view.getContext()).finish();
So, I am able to get the Activity from the View, but not able to call the onResume(), any idea will be appreciable.
Try this, when you press back button using ActivityGroup.
public void back()
{
if ( arrList.size() > 1 )
{
arrList.remove(arrList.size() - 1);
View v = arrList.get(arrList.size() - 1);
Activity_name object = ((Activity_name)v.getContext());
object.onResume();
setContentView(v);
}
else {
this.finish();
}
}
onResume is a lifecycle method called by OS when activity comes to the top of the stack. You can't call it directly.
Related
I need to ensure somehow the activity is on screen visible to user eyes.
We can do this by listetning to onResume & onPause events in Activity class and setting some boolean in these functions.
But onPause is called not only when you minimize your activity, onPause is also called when you switched to other window in multiwindow mode (Android 7.x Nougat)
So the boolean onPause&onResume method will lead to mistakes.
Then how to know visibility of Activity?
Thanks for help.
On Nougat and above you must check the onStop method , it will tell you when your Activity is not visible , you could use this code:
#Override
public void onPause() {
super.onPause();
if (Build.VERSION..SDK_INT <= 23) {
//your activity is not visible
onHidden();
}
}
#Override
public void onStop() {
super.onStop();
if (Build.VERSION..SDK_INT > 23) {
//your activity is not visible
onHidden();
}
}
Assuming that your onHidden method will perform some action.
I found this interesting link about your problem: Multiwindow
I am trying to wrap my head around what proper activity flow convention is.
I currently have:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
//do stuff
//clicklisteners setup etc
Intent intent = new Intent(this, ExampleActivity.class);
//putExtras
startActivity(intent);
}
}
public class ExampleActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
//getExtras
//objectA state lives here
//do stuff
}
}
If the user presses back when on the ExampleActivity view, and then clicks another listener that takes them to ExampleActivity, I want to be able to access "objectA" state again. How do I implement this? I am not understanding onResume or onRestart...
are these the methods to call? or is there a better convention to navigate the app activities?
Android has a mechanism for having an activity pass results back to the prior activity that started it. The documentation for that is here.
Basically, you use startActivityForResult to start the second activity, the second activity uses setResult to set results, and the first activity receives those results in the onActivityResult callback when the second activity finishes.
If the user presses back when on the ExampleActivity view, the ExampleActivity is dead and user is back in the MainActivity, which calls "onResume".
When your are back from activity1 to activity2, activity2's onResume method is called.
With that being said, after the user closed ExampleActivity objectA is destroyed.
When returning to an activity using the back button and
startActivity(new Intent(this, ActivityMainMenu.class));
is called, are there any methods that automatically go to a custom view?
I've noticed that when going back to the view, it's no longer invalidated.
Basically without using the activity's onResume I want to be able to resume my custom view.
For anyone else who wants to know you can use:
protected void onAttachedToWindow()
It's called every time a view is attached to the Window.
In the Android Source for TextView, it posts a Runnable.
if (ss.error != null) {
final CharSequence error = ss.error;
// Display the error later, after the first layout pass
post(new Runnable() {
public void run() {
setError(error);
}
});
}
I want to refresh fragments from an Actvitiy. (Let's call the activity A)
Activity A contains the fragments.
When I finish Activity B then Activity A should refresh the fragments.
I was thinking this would be done in onResume? I tried simply restarting the activity through an intent but that's not usability friendly.
So how do I refresh fragments when I resume an activity?
Just put this code inside
onResume()
For example you want to update your listview when u return to your fregment.
#Override
public void onResume() {
super.onResume();
if (allowRefresh)
{
allowRefresh = false;
lst_applist = db.load_apps();
getFragmentManager().beginTransaction().detach(this).attach(this).commit();
}
}
Here getFragmentManager().beginTransaction().detach(this).attach(this).commit();
This line detach and re-attach the fragment so your content will be updated.
The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume.
#Override
public void onResume() {
super.onResume();
//move your code from onViewCreated() here
}
I agree with Mehul But u can alse do it this way .if you want to replace your fragment you could just say
#Override
public void onResume() {
super.onResume();
if (allowRefresh)
{
allowRefresh = false;
lst_applist = db.load_apps();
getFragmentManager().beginTransaction().replace(Fragment_Container,fragment).commit();
}
}
I want to check to see if an activity is running or finished. Is there any method through which I can check the status of activity?
I found activity.isFinishing() but I am not sure about it.
If you want to perform any step before Activity is going to become invisible.
Their are several choices here.
onDestroy() - for final cleanup.
isFinishing() - right after act.finish() is called it will return true.
onStop() - when the Activity is killed by framework process. (not destroyed)
onPause() - when the Activity is covered by any other Activity
onBackPressed() - capturing the event of hardware Back key triggered by user.
Call isFinishing in the onPause method before the activity is destroyed:
#Override
protected void onPause() {
super.onPause();
if (isFinishing()) {
// Here you can be sure the Activity will be destroyed eventually
}
}
I realize this is very old, but for anyone using Kotlin:
We can check within a Fragment:
// FRAGMENT
override fun onPause() {
super.onPause()
// Log.v(TAG, "onPause");
if(requireActivity().isChangingConfigurations) {
// Config change
} else {
if(requireActivity().isFinishing) {
// deal with activity ending (finish up viewModel etc)
}
// deal with rotate or other config changes as required
}
}
Of course, we can also just do this directly inside an activity (just remove the requireActivity() parts).