Going back to previous activity [duplicate] - java

This question already has answers here:
Android: Go back to previous activity
(24 answers)
Closed 3 years ago.
I have two activities, How is it possible to go back to a previous activity.
What code do I need to go back to previous activity

Assuming you started the new activity with
startActivity(new Intent(this, SecondActivity.class));
puts the SecondActivity in front and the FirstActivity in the backstack. To go back to the FirstActivity, put this in your second activity.
finish();

If you start the activity with result like below :
From 1st Activity :
startActivityForResult(new Intent(this, SecondActivity.class),requestCode);
You can finish the activity with required intents :
From 2nd Activity :
// Optional if you want to pass values from second activity to first
Intent data = new Intent();
data.putExtra("key","any_value");
setResult(RESULT_OK,data);
// Just finish
finish();
Refer the below link for more information like onActivityResult callback and more
https://developer.android.com/training/basics/intents/result

1st Method
If you are using Intent then make sure you don't call finish(); method after using the following code,
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
By this code, the Back Button you created will do its work and go to previous Activity.
And also don't override the onBackPressed() method in Activity.
2nd Method
There is another way that you can achieve this by setting Home Button to go to specific Activity, For that, you have to create Back Button in Action Bar in onCreate() method.
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
And in the AndroidManifest.xml file, you have to add the following,
<activity
android:name="com.example.yourapplication.SecondActivity"
android:label="#string/title_second_activity"
android:parentActivityName="com.example.yourapplication.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourapplication.MainActivity" />
</activity>

using intent
intent = new Intent(this, SecondActivity.class);
startActivity(intent);
Or you can use onBackPressed() if the activity added to backstack
onBackPressed();

Related

Calling startactivityforresult inside activity that has been called by startactivityforresult

I have this issue that I have a list of A and I can add new A, so I startactivityforresult activity with form for A and there is also a button for selecting AType, so I startactivityforresult another activity with AType list.
Problem comes when I do post back result in AType list.
Intent intent = new Intent();
intent.putExtra("aType", aType);
setResult(RESULT_OK, intent);
finish();
And the result is pushed back to A List instead of A Form.
Is this normal behavior or I am doing something wrong?
If this is normal behavior, what can I do to push back result to A Form instead of A List.
If you need more code - I will provide, but I find it now irrelevant.
I have found out that A Form finishes after startactivityforresult call. But why?
Activity1:
Intent intent-=new Intent(this,myclass.class);
startActivityforResult(intent,100);
Override the method OnACtivity result in Activity 1
Activity2:
setResult(RESULT_OK);
finish();
Turned out I have noHistory="true" in AndroidManifest.xml for activity A Form, that is why it was going back to A List Activity

How do we get and set same Toggle value while navigating from one screen(activity) to another activity?

My question is like in my activity 2 i have the screen like below.
I am toggling two or more toggle button (activity 2) and going back(arrow) to activity 1.
Now My question is how to save those value when navigating from activity 1 to activity 2 again for the second time with two or more toggle off.
This is a very general Scenario in Android UI flow:
One way could be saving toggle state in Shared preferences as explained here:
You can save the toggle state in onPause() of Activity in Shared preferences and restore the saved values in onResume() of the Activity
you can transfer infomation from activity1 to activity2 by using intent and bundle
-------------------Activity1---------------click
Intent intent = new Intent(this, Activity2.class);
intent.putExtra("Text", mText);
intent.putExtra("TextColor", mTextColor);
intent.putExtra("TextSize", mTextSize);
intent.putExtra("TextBold", mTextBold);
startActivity(intent);
-----------------------------Activity2--------------oncreate
Bundle extras = getIntent().getExtras();
mText = extras.getString("Text");
mTextColor = extras.getInt("TextColor");
mTextSize = extras.getFloat("TextSize");
mTextBold = extras.getBoolean("TextBold");
in your app, getBoolean is ok

How do I open a new activity with a button click?

I recently started coding in Eclipse and I haven't done much yet so this is more or less my first app. I'm trying to make my school scheudele, it's simple: first activity shows 5 buttons, each button leading to a new activity (monday - friday).
How would I make so that when I click a certain button a new activity (let's say monday) would pop up?
I've seen hundreds of these questions already asked and answered on here but I just don't get it. It's useless to copy & paste code from here if I still don't get what's going on. I know I have to create a new intent and buttonlistener but I just don't get it what for and what to do then.
Could someone explain it to me as detailed as you can how exactly switching between activities using buttons work and how to actually do it?
I have:
MainActivity.java
Monday.java
5 buttons (button1-5)
So how would I code button1 to switch from MainActivity.java to Monday.java?
Start by adding android:onClick="onClick" to each of your buttons' XML elements. This will make your buttons execute the onClick method whenever an onClick event is triggered on them.
Then in your MainActivity class add the following method:
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
// Monday
Intent intent = new Intent(MainActivity.this, Monday.class);
startActivity(intent);
break;
case R.id.button2:
// Tuesday
Intent intent = new Intent(MainActivity.this, Tuesday.class);
startActivity(intent);
break;
// the rest of the buttons go here
default: Log.e("YourTAG", "Default in onClick hit!");
break;
}
}
So every time there is an onClick event on any of your five buttons, the onClick method above will execute with the argument representing the View you just clicked on.
Details regarging intents and how they work here
And as #Edward noted, don't forget to add your new activities in your AndroidManifest.xml file under the application element, such as:
<activity android:name=".Monday" android:label="#string/app_name"></activity>
what you should do is to create an Intent that will fire your Activity you do that by this code:
startActivity(new Intent(YourCurrentActivity.this, Monday.class));
You will have to fire a different Intent on each button that will create the appropriate Activity.
Of course don't forget to declare your Activitys in your manifest file.
OK. Lets say you have following button in your layout file:-
<Button
android:id="#+id/my_btn"
android:layout_width="55dp"
android:layout_height="22dp"
android:onClick="goToMonday" // function name which will be in MainActivity.java
/>
Now in your MainActivity.java:-
public void goToMonday(View v){
Intent monday_intent = new Intent(MainActivity.this, Monday.class);
MainActivity.this.startActivity(monday_intent);
}
This is the way to change activities.

Android activity stack control. Can't figure out how to remove history

I have following 2 activities(plus many more not important for this question):
<activity android:name=".activities.HomeActivity" android:excludeFromRecents="true" />
<activity android:name=".activities.AdHocActivity" android:noHistory="true"/>
HomeActivity is a first one and only one wich I keep history for. User can go differnt places from Home and click Back to come back to main HomeActivity.
I also have service running on alarm and checking for some specific things. When specific criteria met I'm displaying my AdHocActivity
Intent i = new Intent(context, AdHocActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra(BlockingActivity.INTENT_BLOCKED_PACKAGE_NAME, packageName);
context.startActivity(i);
FLAG_ACTIVITY_NEW_TASK was necessary to show Activity from BroadcastReceiver
Now, this AdHoc activity displays some message to user and has a button to take user back to HomeActivity
private void sendToMainApplication()
{
Intent i = new Intent(this, HomeActivity.class);
startActivity(i);
finish();
}
All this work, but I get second instance of HomeActivity in a stack. So now when user taps "Back" - my Home activity flashes and comes back (previous copy).
I want only one copy to stay on top. I want it to be like anchor one. If it doesn't exist - I want new one to be opened.
The trick to prevent HomeActivity from being doubled was to call it like this:
private void sendToMainApplication()
{
Intent i = new Intent(this, HomeActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}

Android. How to start activity without creating new one?

I tried to set attribute android:launchMode="singleTask" for calling Activity, but it still does not works as I expected.
I need that method onCreate(Bundle bundle) to be called only once, but it still calls each time when I start Activity.
I start Activity using code like this:
public void onClick(View v) {
Intent myIntent = new Intent(v.getContext(), NextActivity.class);
startActivity(myIntent);
}
Please let me know what I am doing wrong
It must be like this:
android:launchMode="singleTop"
and calling:
Intent intent= new Intent(context, YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
Already existed topic about this: Android: new Intent() starts new instance with android:launchMode="singleTop"
Even if you make your launch mode 'singleTop', a new activity will be started.
At each activity creation, its onCreate() will be started.
My understanding is that the singleTop option, basically finishes the caller Activity.
I think that you may think that onCreate() is a form of Application constructor but it really is an Activity constructor. You may want to do your one time initializations elsewhere.

Categories