My mainActivity contains 5 fragments which you can switch through with tabs. The main tab shown on start is the third one (the one in the middle). I have a button in fragment #1 which opens activityTwo. What should I put in the onBackPressed method in activityTwo in order for it to bring me back to mainActivity with selected tab #1. I currently have this, but it opens mainActivity and shows the main tab (#3)
#Override
public void onBackPressed() {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
ActivityTwo.this.finish();
}
How can I make it show tab #1 instead of tab #3?
Your first activity should still be on the activity stack, so you should not call startActivity again. Instead, you can use setResult to pass a value back to your first activity:
// Inside your second activity
#Override
public void onBackPressed() {
setResult(RESULT_OK);
finish();
}
Then, from your first activity, replace startActivity with startActivityForResult, with a request code (any integer you want, as long as it's unique):
startActivityForResult(intent, YOUR_REQUEST_CODE);
Then override onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == YOUR_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Code to select tab 1 here
// mTabLayout.getTabAt(0).select();
}
}
}
i think you are using view pager , so while you return from activity 2, use viewPager.setCurrentItem(page);
where page will be your tab no, (in this case it will be 2, since it starts with 0). Hope this helps, if not then please clarify a bit more.
Related
I have a problem with Shared Element Activity Transition between activities. I have MainActivity, it has a recyclerview with boxes (recyclerview.horizontal). Each box when clicked will go to the corresponding activity. The problem that appears when I click on a box, I switch to the second activity, in the second activity I press a button to switch to the third activity. And here I swipe to right to return to the MainActivity with transition and I want it to transition right to the box corresponding to the 3rd activity in the recyclerview in MainActivity. So, my purpose is:
MainActivity (Shared Element Activity Transition)-> Second Activity ->
Third Activity (Shared Element Activity Transition)-> MainActivity
(exactly scroll to position for Third Activity in RecyclerView).
My MainActivity
I hope everyone offer me the solution. Thank you so much.
You can use startActivityForResult instead of startActivity in SecondActivity when you are going to start ThirdActivity.Like this :
Intent i = new Intent(this, ThirdActivity.class);
startActivityForResult(i, 1);
And when you are finishing your ThirdActivity
Intent returnIntent = new Intent();
returnIntent.putExtra("activity_finish",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
If you use startActivityForResult() then it gives callback in the Activity who started it,so as soon as ThirdActivity finishes, it will return to the onActvityResult() in SecondActivity.Where you have to check result_code and request code like this :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == Activity.RESULT_OK){
boolean isActivityFinish=data.getBooleanExtra("activity_finish");
if(isActivityFinish){
// finish your Second Activity here
}
if (resultCode == Activity.RESULT_CANCELED) {
//Write your code if there's no result
}
}
}
For more info : How to manage `startActivityForResult` on Android?
In my app, there are 3 activities and their calling sequence is like this...
Splash activity--->Dashboard Activity--->List Activity
If it goes like this then If I press back button on List activity then it will follow the reverse sequence of above.
There is one requirement where user can navigate to List Activity directly from Splash (Skipping Dashboard Activity) Now when user will hit back button on List Activity then I wan't to show Dashboard Activity which is not there in the Activity Stack.
So please help me with the best approach.
Pass a boolean through the intent for going to List Activity from either of the others. Using onBackPressed check if the boolean is true or false for skipping Dashboard Activity.
Then if true put new intent for loading dashboard activity and finish(); on list activity.
You have to pass class name as intent extra from both Splash and DashboardActiviy.
In List Activity you have to get the class name using getIntent().
When the user click back button, you need to check the class name based on that you can take decision.
if(name.equalIgnorecase(DashboardActivit.class.getSimpleName()){
//Add your intent
}else{
//
}
This may give you definite solution to you.Give a try
you can directly go to splash from list Activity while going to
ListActivity from Dashboard Activity call finish()
Intent i = new Intent(DashboardActivity.this,ListActivity);
startActivtiy(i);
finish();
Start your inner activities with startAcvitiyForResult
Intent i = new Intent(this, Activity_2.class);
startActivityForResult(i, 1);
and in your inner activity
#Override
public void onBackPressed ()
{
finish();
}
You can also do stuff in your outer activity as you like after your inner activity finishes
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == 1){
Log.i("TEST", "RESULT_OK");
}
else {
return;
}
}
}
I have a viewpager, and I'm using FragmentPagerAdapter. Now, the tab 0 of the FragmentPagerAdapter has an option that takes the user to a fragment, call it fragment B --It replaces the fragment of tab 0 with Fragment B, within the viewpager itslef. This Fragment B has an option that opens up an activity, now when I press back when in the activity, I'd like to go back to tab 0, rather than Fragment B. How do I achieve that?
Tab 0's fragment calling Fragment B:
public void onClick(View view) {
TagFrag f = new TagFrag();
getFragmentManager().beginTransaction()
.add(R.id.note_container, f).addToBackStack(null).commit();
}
UPDATE 2:
This Repo demonstrates this answer with a concrete example (for Android Studio 1.0.1)
UPDATE 1: NOTE ANSWER EDITED: onActivityResult should be placed on FragmentB instead of FirstActivity
In FragmentTab0, add FragmentB ilike this:
getFragmentManager().beginTransaction()
.add(R.id.note_container, f).addToBackStack(null).commit();
In FragmentB, Open the second activity using startActivityForResult
Intent intent = new Intent((FirstActivity)getActivity(), SecondActivity.class);
startActivityForResult(intent, 1234); // 1234 is the RequestCode
In SecondActivity, override onBackPressed
#Override
public void onBackPressed() {
setResult(Activity.RESULT_OK);
super.onBackPressed();
}
In FragmentB override onActivityResult and call [getFragmentManager().popBackStack()][2]
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1234) { // previously defined request code
// Make sure the request was successful
if (resultCode == RESULT_OK) {
getFragmentManager().popBackStack(); // magic happens here.
}
}
}
Enjoy!
How can I detect if an activity came to focus after pressing the back button from a child activity, and how can I execute some code at that time?
One possibility would be to start your child activity with startActivityForResult() and implement onActivityResult() which will be called when you return from the child activity.
js's answer is right, but here is some debugged code.
Declare the request code as a constant at the top of your activity:
public static final int OPEN_NEW_ACTIVITY = 12345;
Put this where you start the new activity:
Intent intent = new Intent(this, NewActivity.class);
startActivityForResult(intent, OPEN_NEW_ACTIVITY);
Do something when the activity is finished. Documentation suggests that you use resultCode, but depending on the situation, your result can either be RESULT_OK or RESULT_CANCELED when the button is pressed. So I would leave it out.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == OPEN_NEW_ACTIVITY) {
// Execute your code on back here
// ....
}
}
For some reason, I had trouble when putting this in a Fragment. So you will have to put it in the Activity.
The method you're looking for may be the onResume method you can implements in your mother class ;). You must know that the onResume is also called the first time you launch any activity. Look at the lifecycle of an activity : http://developer.android.com/images/activity_lifecycle.png
Regards,
You can also override both the onBackPressed() method and the onOptionsItemSelected() method and put some logic there. For example I put this into my BaseActivity which all the other Activities extends from:
#Override
public void onBackPressed() {
// your logic
super.onBackPressed();
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
// your logic
}
return super.onOptionsItemSelected(item);
}
In an given Android activity, I would like to start a new activity for the user at some point. Once they leave the first activity and arrive at the second, the first activity is hidden..
Now my question is
I want to bring back the first activity (i dont want to create a new instance of the first activity but to bring back the already existing instance of the first activity) when a button is clicked in the second activity ...
thanks :)
so simple. integrate the below code in your second activity
Button b = (Button)findViewById(yourbuttonid here);
b.setOnClickListener(new View.onClickListener(){
public void onClick(View v){
finish();
}
});
This will work
You would define the first activity with launchMode="singleInstance", then you would start the activity as usual.
Depending on the usage of your second activity, you could also use startActivityForResult() when you start your second activity...
FirstActivity.java{
private static final int SECOND_ACTIVITY = 0;
openSecondActivity(){
Intent forChildIntent= new Intent( this ,FirstActivity. class );
//data for second activity
forChildIntent.putExtra("userName", getUsrName());
this.startActivityForResult(forChildIntent, SECOND_ACTIVITY);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch (resultCode) {
case RESULT_OK: //do something
default:break;
}
}
SecondActivity.java{
goBackButtonClick(){
Intent retData=new Intent();
//set data to pass back ,if required
//retData.putExtra("userName", getUsrName());
setResult(RESULT_OK, retData);
finish();//will take you to the first activity
}
}