Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Calling Activity within itself

I am trying to make an app which have 2 main controls (along with other info fields) 1.> Next button 2.> Done button

I want to call the same activity when next button is pressed and display some other activity when Done button is pressed

Done button is working fine. But when I press Next button the app stops working. The error that I get is : Unfortunately, myapp has stopped working

This is the same error which I usually get when I don't define activity in manifest file. Can anyone please help me with this problem.

And finally Is is legitimate to call same activity within itself ?

Thanks

like image 390
Harshit Syal Avatar asked Jun 13 '12 08:06

Harshit Syal


People also ask

Can an activity start itself Android?

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.

How can I get call activity in Android?

If you start the activity with startActivityForResult(Intent, int) , then you can get calling activity by getCallingActivity(). getClassName() .

How can I call activity from another activity?

Start the Second Activity To start an activity, call startActivity() and pass it your Intent . The system receives this call and starts an instance of the Activity specified by the Intent .

When onDestroy is called in Android?

onDestroy( ) is called before the activity is destroyed. The system invokes this callback either because: the activity is finishing (due to the user completely dismissing the activity or due to finish( ) being called on the activity), or.


2 Answers

I think this should work

Intent i= new Intent(ActivityA.this,ActivityA.class);
like image 170
Nirali Avatar answered Sep 20 '22 19:09

Nirali


You can use Intent flags to call the activity again. In the button click

setContentView(R.layout.main);
Intent intent= new Intent(this,SameClass.class); 
startActivity(intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT));
like image 37
SKT Avatar answered Sep 18 '22 19:09

SKT