How to open activity to certain tab? - android

I have a Main Activity which a FragmentActivity and I have three fragments which are on three separate tabs. On fragment two, I have a button which opens an an activity on which the user can click OK or cancel. Right now when he clicks OK or cancel nothing happens and it just quits the application.
What I would like it to do, is that in onClick() (of either the OK or cancel buttons) of the activity that was opened from the fragment, it goes back to the original activity and back to the same fragment it was on, in this case fragment two.
I right now I have it so that it goes back to the original activity but the first fragment, not the one from which the button was pressed...how can I accomplish this? Here is what I have to go back to the original activity:
btnOK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//execute background task for processing
new SaveDetails().execute(); //this actually calls an HTTPRequest
Intent a = new Intent(ACTIVITY_Edit_Trip.this, ACTIVITY_MAIN_CONTROLLER.class);
startActivity(a);
}
});
The tabs are working fine and i can swipe between them, I just don't know how to go back to a specific one from an activity. Is this possible?
Thanks for the help! If I need to post my PagerAdapter code, I can do that, just let me know!
Edit: I have two activities, the main activity and an edit activity. My main activity is a FragmentActivity which is also the one that creates the tabs where the fragments live one. The edit activity is accessed only when the user presses a button from fragment 2.
Once the button on fragment two is pressed, the edit activity opens and it has two buttons OK and cancel, if the user presses either it should go back to the main activity > fragment 2. Right now with the code I have posted, it goes to main activity > fragment 1.

Related

close fragment and activity B at the same time (go back to activity A)

I am working on an application with specific architecture regarding navigation. The situation I have right now is like this:
I have activity A. My activity opens activity B with function startActivityForResult().
After that on activity B is added fragment X.
Right now I am on fragment X and I detected the close button click and I transferred this event to activity B. Now I want to go back to activity A. The only way I see at this moment to achieve this is by calling 2 times function onBackPressed() in a row. This creates laggy animation in which the user sees removing of fragment and activity B in delay. This is bad for UX and looks bad in code.
Does anyone have some better suggestions?
on your fragment X button click.
button.setOnClickListener{ (activity as ActivityB).finish() }
or
button.setOnClickListener{ requireActivity.finish() }

Android phone built-in back button

I have an activity with two fragments, each fragment has a back button to the previous activity/fragment. Both the back buttons on the fragments work properly. However when i run the app on my android phone and i use the built-in back button to navigate to the previous activity, it displays a blank activity and then when i press the built-in back button again only then does it navigate to the previous activity. The problem is clearly the back burton than built in.Is there a way to solve this???
There is a method in the Activity called onBackPressed() which is called when the device back button is pressed. If you want to control what happens on back press just override it. To remove default onBackPressed action you need to remove the call to super.onBackPressed() and then you control what happens when back button is pressed.
#Override
public void onBackPressed() {
//super.onBackPressed();
// do something here
// or perhaps nothing at all
}

Going back to fragment recreates and adds to existing dataset

I have a Fragment and I create some data inside onCreate(). From this fragment I can go to another one by clicking a Button.
The problem starts when I click the back button. What happens then is that it goes again through onCreate() and re-creates a new dataset and adds it to the old one.
The result is that I end up with two datasets instead of one. How can I skip onCreate when I'm coming from back button or is there another way ?
#Override
public void onBackPressed() {
//Check if you're in the button pressed-fragment
if(){
//You're code here
}
}
This overrides the backButton and you can do what you want - create new fragments or load the from backstack and other stuff. Be careful though, people don't like to not being able to quit the app with back button.

Going back to my main activity, and refreshing the fragments

So, my application have one activity ProjectActivity as a home, which contains 3 fragments HomeFragment ListFragment TagFragment(one for each tab). When I click on a button (on the homeFragment), it opens a new activity CaptureActivity.
That activity launches an ASyncTask, ParseData.
On the ParseData onPostExecute, I would like to go back to my ProjectAcitity, focused on a specified tab (the one that contain ListFragment), and update the ListFragment View.
I actually have no clue on how to do that. Do I simply need to call the ProjectActivity ? But then, if I press the Back button, wouldn't it go back through the whole chain (Project Activity, CaptureActivity, ProjectActivity) ?
Well, you can do a few things.
First just have ListFragment monitor w/e its listing and refresh it in its onStart/onResume which will be called when you return to your ProjectActivity.
Or more directly you could do startActivityForResult(captureActivity); then when u come back to your ProjectActivity get results in onActivityResults() then update your ListFragment from there.

How do I return to a calling activity if the user presses BACK?

My MAIN activity is spawning a child activity that contains a ListView. While this ListView is being populated (through an AsyncTask), an indeterminate progress bar is shown.
However, assuming that I am an impatient user and I press the BACK button, the progress bar is cancelled but I am left with a blank screen. I have to press BACK one more time to go back to the MAIN activity.
I would like the app to go back directly to the MAIN activity by pressing BACK only once. Can somebody point me in the right direction? I am thinking I should call finish() somewhere but I don't know where to put it. Thanks in advance!
Use setOnCancelListener() on Dialog (which ProgressDialog extends).
you should override "onBackPressed()"
it will call when the activity has detected the user's press of the back key.
#override
public void onBackPressed(){
this.startActivity(this,MainActivity.class);
}
this code will call the MainActivity when emulator/ipphone back button pressed...
you can try like this,place this code in your activities oncreate block.
findViewById(R.id.back).setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
}
});

Categories

Resources