When creating a new Intent to start a new activity, is it possible for an activity to call itself and is this good program technique. For example, let's say I have a template for an activity and to avoid making 10 different activities, would it be handy to have the same activity call itself?
An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.
However, activity can't be run unless it's on foreground. In order to achieve what you want, in onPause(), you should start a service to continue the work in activity. onPause() will be called when you click the back button. In onPause, just save the current state, and transfer the job to a service.
An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .
1.2. To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent.
Yes it is. If your requirement are like that then there is no harm in doing that. If you use this then dont forget to call finish(). finish() will remove the activity from backstack so when you press back you dont return to previous instance of same activity.
startActivity(new Intent(MyClass.this,MyClass.class));
finish();
Yes You can do that, but then you should consider onBackPressed() behaviour as you dont want same activity comes up from your stack when user keeps on pressing back. you may use intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP ); to make sure only one instance is created of same activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With