Android, Calling Activity within itself - android

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

I think this should work
Intent i= new Intent(ActivityA.this,ActivityA.class);

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

Try this
Intent intent= getIntent();
finish();
startActivity(intent);
Thank you

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.

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

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

Related

Back button from activity 2 closes down the app instead of going to activity 1 in Android 7

Pressing back button from the second activity returned to the first activity before without problems. I then updated to Android 7.
Then the whole app closed when pressing back button from the second activity.
I know that there are threads about this here and I have checked them all. Basically, they say that finish() should be avoided from the first activity.
I don't call finish(), so that is the problem here. It is difficult to solve, because it works like it should when I launch the app from Android studio.
It returns to the first activity from second. The problem occurs when the app is started by pressing its icon (not from Android studio).
Pressing back from the second activity closes down the whole app. How can I solve this? Here is some of my code:
Activity 1:
Intent glIntent = new Intent("astral.worldstriall.GLActivity");
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
The below code should be used in 2nd activity so that when u press back button it terminates the current activity(2nd activity) and goes back to previous activity
#Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
I think you just misused the Intent constructor. According to the documentation, you used this constructor Intent(String action). The one that you atually want should be this one Intent(Context packageContext, Class<?> cls).
In the first activity (therefore this being the instance of your first activity), you should write:
Intent glIntent = new Intent(this, astral.worldstriall.GLActivity.class);
glIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(glIntent);
For going first to second activity...
Intent i=new Intent(FirstActivity.this,SecondActivity.class);
startActivity(i);
Use below code for going in previous activity...
#Override
public void onBackPressed() {
super.onBackPressed();
Intent i=new Intent(SecondActivity.this, FirstActivity.class);
startActivity(i);
}

Go to fist activity when open many activity

In my app I have two fragment in ManiActivity.class .
In my fragment I have a listview , when I click a item in list I open MainActivity2.class . In MainActivity2.class I have listview related and when i click list related I open MainActivity2.class with new value. Now I want create a button back home in MainActivity2.class and when I open many MainAcitivty2.class when click list related on it I can click button back home to go to MainActivity2.class
I try it with
Intent intent = new Intent(MainActivity2.this,MainActivity.class)
startIntent(intent)
But when I click back button in MainActivity.class it comback MainActivity2.class , it not exit app.
How I do to clear it?
Please help me.
This is why Android maintains states of each activity in a Stack, that's why when you will press that Back-button, you just have to clear all previous activities from the stack and then open MainActivity
Just you have to set flags before starting intent:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Use android:launchMode="singleTask" for your MainActivity in AndroidManifest.xml.
Which will keep only one instance of MainActivity if launched again this activity will come to top of the stack.
And Use this code
Intent intent = new Intent( MainActvity2.this, MyActivity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP );
current_activity.startActivity( intent );
call finish method after startIntent
something like this:
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
and put finish method in your activity
#Override
public void finish() {
super.finish();
}
its very simple.. use finish();
Intent intent = new Intent(MainActivity2.this,MainActivity.class);
startIntent(intent);
finish();
bcz u create multi activity in mainActivity2. so u have to destroy your activity before u go to mainActivty.
otherwise when u open new many in mainactivity2. that time use finish.. bcz u must have to destroy first your mainActivity2.
if you not understand my answer then just post your whole code... i will edit...

Returning To Main Activity

My application has three activities. The main activity, a play game activity, and a game over activity. The main activity starts the play game activity after a button is pressed:
Intent myIntent = new Intent(view.getContext(), PlayGameActivity.class);
startActivity(myIntent);
Inside the play game activity, when the game is over, I start the game over activity the same way:
Intent myIntent = new Intent(view.getContext(), GameOver.class);
startActivity(myIntent);
Inside the game over activity when the user clicks the hardware back button I want it to return to the main activity (not the play game activitiy which is what's currently happening).
How do I do this?
On your Manifest add
android:noHistory="true"
to your PlayGameActivity
#Override
public void onBackPressed() {
Intent intent = new Intent(GameActivity.this,MainActivity.class);
startActivity(intent);
// finish(); // un-comment <--- this to close the previous activity if you don't want to use it anymore.
}
this will handle the back button. just add it in your game activity class.
please do accept the answer if its what you seek for.
As #Hoan Nguyen stated. you can add android:noHistory="true" to your gameactivity in the AndroidManifest.xml file.
or you could finish your game activity once you started the GameOver Activity. and the code should look something like this:
///on Game Over
Intent intent = new Intent(GameActivity.this, GameOverActivity.class);
startActivity(intent);
finish();
Those ways you will get back to the original instance of your MainAcitivty instead of lunching a new one.

How to connect GUI screens in Android ?

How to connect GUI screens in Android? I have created several screens and I want to connect these using buttons.
It depends what else you want to do by clicking a button. If you only want to click a button and go to next activity or you want to send some data to next activity. Or maybe you want to dial a number or view contacts etc.
Here's a nice tutorial about using startActivity() and startActivityForResult() methods - android startActivity and startActivityForResult
Also basic Intents Tutorial will be useful.
just take a event handler .. in that use INTENT
Here s the syntax , startActivity(Intent(activity1.this, activity2.class);
ex:
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(activity.this, activity2.class);
startActivity(i);
}
});
Here !st activity1 i..e.. ur current class(1st page) name
Intent is the best mechanism to connect your component(activties, services) run time. here you can find about intent class.

Returning to the Home Screen and then returning to last activity

Having real issue understanding how to sort my issue out.
On the Home screen I have 2 buttons
When the user clicks the first button it starts a new Activity. What I am looking for is if the user clicks back the app returns to the home screen. If the user clicks the first button again it starts a new activity.
If the user clicks the second button it returns to the activity that was last started by clicking button 1
What I am having issue with is how to save the state of the activity when the user clicks back
Also how to call that activity when the second button is pressed
Thanks for your Time
UPDATE
I have gone down part of this but still having issues. If I put some of the code I am using perhaps someone can point where I gone wrong.
Code for calling the new activity from main menu
Intent intent = new Intent(MainMenu.this, NewClass.class);
intent.putExtra("value1", value1);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Within the new class I have added the following :
#Override
public void onBackPressed() {
//super.onBackPressed();
Intent intent = new Intent(RoundScoring.this, MainMenu.class);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Toast.makeText(this, "Back Button Pressed", Toast.LENGTH_LONG).show();
}
I do not have either a onrestoreinstancestate or onresumne in this class. only a oncreate. Do I have to add something like this to bring back the instance
On the second button on the main menu I have added this
Intent intentContiune = new Intent(MainMenu.this, NewClass.class);
intentContiune.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentContiune);
Thanks
Try this:
Home Screen Left Button:
Open the new activity with an intent flag, FLAG_ACTIVITY_NEW_TASK
Activity:
Override onBackClick() on the started activity to call the home screen with an Intent instead of finishing it. Use the flag FLAG_ACTIVITY_REORDER_TO_FRONT
Save activity state overriding OnSaveInstanceState
Home Screen Right Button:
Call the activity with the flag FLAG_ACTIVITY_SINGLE_TOP
More info about flags:
http://blog.akquinet.de/2010/04/15/android-activites-and-tasks-series-intent-flags/
One solution may involve passing bundles around that include the state of your activity. Using startActivityForResult(), you can return a bundle with the activity's state. When the user clicks your second button, pass in that bundle and have the activity set itself up with respect to the contents of the bundle. If the bundle doesn't contain the information you're looking for, then use the default values as if you were just starting it.
For more information:
Android: Capturing the return of an activity

Categories

Resources