Harshit Syal
Harshit Syal

Reputation: 659

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

Upvotes: 6

Views: 17026

Answers (6)

Ramesh R
Ramesh R

Reputation: 7077

Intent i= new Intent(ActivityA.this,ActivityA.class);
startActivity(i);

or

startActivity(new Intent(ActivityA.this,ActivityA.class));

Upvotes: 0

Muhammed Fasil
Muhammed Fasil

Reputation: 8576

Try this

Intent intent= getIntent();
            finish();
            startActivity(intent);

Thank you

Upvotes: 2

Vineet Shukla
Vineet Shukla

Reputation: 24031

Why do you need to call the Activity within itself?

You can do following things:

1. You can reset data on next button click.

2. You can hide view or make visible on next button click.

Clear your requirements and show your code to check why the error Unfortunately, myapp has stopped working is coming.

Upvotes: 1

SKT
SKT

Reputation: 1851

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));

Upvotes: 10

Nirali
Nirali

Reputation: 13825

I think this should work

Intent i= new Intent(ActivityA.this,ActivityA.class);

Upvotes: 11

hemanth
hemanth

Reputation: 1

Use TabGroupactivity

In the next button onclick write

public void next(View v) {      

    Intent next= new Intent(getParent(), next.class);
    TabGroupActivity parentActivity = (TabGroupActivity) getParent();
    parentActivity.startChildActivity("next", next);

}

Upvotes: 0

Related Questions