Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an Android activity call itself? [closed]

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?

like image 881
user3774329 Avatar asked Jun 27 '14 11:06

user3774329


People also ask

What does Android activity mean?

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.

Can activity run in background Android?

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.

What is the life cycle of Android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() .

How do you start an activity from itself?

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.


2 Answers

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();
like image 116
Illegal Argument Avatar answered Sep 24 '22 04:09

Illegal Argument


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.

like image 26
vivek Avatar answered Sep 24 '22 04:09

vivek