Returning To Main Activity - android

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.

Related

Activity trigger more than once if using launchMode="singleInstance

I have a activity which i declare as launchMode="singleInstance in Manifest file. and using blow code to starting this activity:
Intent intent = new Intent(this, LockScreenActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
To close this activity i am doing like: finishAffinity();
But some how when i finish this activity i can see another open activity instance of this. Can someone tell me how i can finish all OR only create a single one when i need to start this activity from different places inside my application?

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

Does the Intents get killed after i pass to another Intent Android

I'm working on an android application and in the application I have a couple buttons that let user to pass to another activity. Now the way I'm doing the transitions between this Intents is like below:
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
With the above content I start an activity and from that activity I'm getting back to the MainActivity using this code:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
But i suspect this cause memory to be over used because I'm not actually getting back to the Main Activity that created when application started, I'm just creating another instance of MainActivity I guess. Is this really as i thought and if it is how can I get back to the activity that created in the beginning or if I can't do such thing how can I make app to let the previous activity go?
Passing an intent to startActivity() will create a new instance of the activity and add it to the front of the stack. So:
Intent intent = new Intent(context,MainActivity.class);
startActivity(intent);
is basically asking to create a new instance. If you want to go back to the activity just before the current one, call either:
finish();
Or,
super.onBackPressed();
In your solution you just have to press back button and you'll be back in first activity.
If you want to close it and after open new instance like you are doing in second activity just add
finish();
at the end of
Intent intent = new Intent(this,user_area.class);
intent.putExtra("user",user_name.getText().toString());
startActivity(intent);
You just need to call finish(); method
Intent intent = new Intent(this, DestinationActivity.class);
startActivity(intent);
finish();

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...

how to close android app when back button pressed

I am building a simple login application on android platform.
The 1st Activity is a Login Activity and the 2nd one is a welcome screen.
When a user presses the login button, I start the 2nd Activity by using an Intent.
But in the 2nd Activity, when the user presses the back button, it opens the 1st Activity.
But at this point I want to close my app.
Can I use Fragments?
Any other alternative?
you have to just finish your activity when you switching another activity,
Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();
you call the finish() in LoginActivity when you start the your SecondActivity
Intent i = new Intent(Login.this, Welcome.class);
startActivity(i);
finish();
OR
you set the android:noHistory = "true" in LoginActivity in AndroidManifest.xml.
you need to create your second activity with FLAG_ACTIVITY_CLEAR_TOP from your first activity.
and then in your second activity you need to call finish() in onbackpressed()
#Override
public void onBackPressed() {
finish();
}

Categories

Resources