OnBackPressed in Xamarin Android - android

I have:
public override void OnBackPressed ()
{
base.OnBackPressed ();
}
How can I get previous state of my app?
Does Xamarin have such as
BackStackEntryCount, how can I use it?
For example I click MainMenu->Study->Categories->FirstCategory, if I click Back, I want to have Categories.

If the Categories activity precedes the Selected category in the activity stack and is not flagged for no history OnbackPressed without override of the base/super class will take you back to the categories class and hit the OnResume method.
If you loaded your Categories activity in OnCreate and dont do anything in OnResume then OnBackPressed should show the previous activity in the state you left it.
If you want it to do something else then override on OnBackPressed and flag Categories as no history.
Override OnBackPressed
public override void OnBackPressed()
{
//Do not call the base method
//base.Onbackpressed
//Do something else
*something else code*
//finish this activity unless you want to keep it for some reason
finish();
}
NoHistory
[Activity
(
NoHistory = true
)
]
Public Class Categories : Activity

public override void OnBackPressed ()
{
finish();
}//end onBackPressed()

Related

overriding back method in fragment class in android

how to quit the app whenever i press the back button from certain fragments.I found out that system.exit(1) closes the app. but i could now override method for handling back key.Is there any method to override back key ? if yes How can i perform it?
onBackPressed() function in activity will help
write this in your onBackPressed()
if (fragmentManager.getBackStackEntryCount() == 1) {
if (isPressed) {
finish();
} else {
StringUtils.displayToastShort(this, "press again to exit");
isPressed = true;
}
}
What you should do, is when you add a fragment add a tag with it, like
fragmentTransaction.replace(android.R.id.content, fragment, "My_Tag");
And then in activity onBackPressed()
fragment= (AddFriends)getFragmentManager().findFragmentByTag("My_Tag");
if (fragment!= null && fragment.isVisible()) {
//Exit from your app here
finish();
}
Hope this helps you.
Try this function for opening a fragment
public void openFragment(Fragment fragment)
{
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransac‌​tion();
fragmentTransaction.replace(R.id.fragment_container,fragment);
fragmentTransaction.commit();
}
and when you want to open a fragment call this function like this
openFragment(new AddFriends());
Using above function it will exit the app with back button press.
I solved it in following way.
First make a class with static integer variable(say a).
Override back methods on mainActivity and choose the fragment you want to open on back pressed as per the value on static variable.
[note:whenever new fragments are opened update static variable with new value and according to this value override back method on main activity]

Setting a another view to load when touch back(hard key) in android

I want to navigate to another screen when user touch back button. I found a method for this.
#Override
public void onBackPressed() {
// do something on back.
return;
}
I'm navigating from a fragment.But it is not clear that where I have to use this and how. Please help.
You'll need to add that method to the Activity class of the screen you're navigating from.
Then, in that method, add your intent code to move to your second Activity (let's call that Activity2 below) using startActivity
#Override
public void onBackPressed() {
Intent intent = new Intent();
intent.setClass(this, Activity2.class);
startActivity(intent);
}
Just override the method onBackPressed and navigate to the screen you want to via intents or whichever way you prefer.
#Override
public void onBackPressed() {
getFragmentManager().beginTransaction.replace(thisfragment,new Fragment).commit();
}
The above code will replace your current fragment with the new fragment to which you want to navigate.

How to reload the fragment page when I was in an activity and used finish() method?

Let's say I got a fragment page called "A" and "B" and an activity called "C".
When I click the add button in Fragment B, it will takes the user to activity C. When the user input all the information in activity B and press submit, it will take the user back to fragment B and reload it.
My situation is I use the finish() function on "C" while I press the submit but when it go back to the fragment B it doesn't refresh.
My code:
private View.OnClickListener mSubmitButtonOnClick = new View.OnClickListener(){
#Override
public void onClick(View v)
{
if(getArray() != null) {
insertRecord(getArray());
finish();
}
else{
Toast.makeText(AddClaimFormPage.this, "Please select at least one item to claim.", Toast.LENGTH_SHORT).show();
}
}
};
Please help, how can I reload the fragment B when I used finish()?
You need to override the onResume() on fragment B:
#Override
public void onResume()
{
super.onResume();
// Load data and do stuff
}
Refer Fragment Lifecycle for more details.
you have to manage stake of fragment and when you back from another activity to previous activity than load fregment which is the top on stack.

Finish the activity using component name in android

I am trying to finish one activity from another.
For that purpose I am having only the component name of that activity.
How can i finish that ?
1.Make your activity A in manifest file: launchMode = "singleInstance"
2.When the user clicks new, do FirstActivity.fa.finish(); and call the new Intent.
3.When the user clicks modify, call the new Intent or simply finish activity B.
FIRST WAY
In your first activity, declare one Activity object like this,
public static Activity fa;
onCreate()
{
fa = this;
}
now use that object in another Activity to finish first-activity like this,
onCreate()
{
FirstActivity.fa.finish();
}
SECOND WAY
While calling your activity FirstActivity which you want to finish as soon as you move on, You can add flag while calling FirstActivity
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
But using this flag the activity will get finished evenif you want it not to. and sometime onBack if you want to show the FirstActivity you will have to call it using intent.
You can do it in very simple way.
First create a static instance of your activity e.g. MainActivity, whom you want to finish like,
public static MainActivity act=MainActivity.this;
and now in another actvity e.g. MainActivity2 just call this line,
MainActivity.act.finish();
Try extending that activity and override the finish method
public class ma extends MainActivity{
#Override
public void finish()
{
super.finish();
}
}
You want to exit application after log out.
that time to user this
i.addCategory(Intent.CATEGORY_HOME);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
or try to another way like
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
moveTaskToBack(true);
finish();
}

Activity group does not refresh activities when back button is clicked

I have a tabhost on my application and I'm using an Activity group which handles 3 activities inside.
Example:
ActivityGroup Handles
A -> B -> C
When i start this activities i'm using the flag Intent.FLAG_ACTIVITY_CLEAR_TOP.
My problem is when the user goes from A->B->C and press back button, my B activity shows up, but it does not resume or reload or refresh. It has the same state as before.
For example if the user goes again to C, C is refreshed, but when from C goes back.... B is not.
On B I have implementend methods such as onResume, onStart, onReestart and debugging it the main thread never goes in there...
And i need to refresh B because C can make changes that change the content displayed on B.
I have googleled this for 3 days and I couldn't found a solution..
I had this problem too.
I was using ActivityGroup code based on this blog post.
When I pressed the back button the pervious View would load fine, but the activity associated with it would not fire the onResume().
I was using an extended activity with on overridden and public onResume().
I found this blog post, so tried casting the view as my extended activity and called onResume().
Bingo.
Edit.... here's some more detail...
public class YFIMenuListActivity extends ListActivity {
....
#Override
public void onResume() {
super.onResume();
}
....
}
onResume() is normally protected, but I override it and make it public so that my ActivityGroup can call it.
I only have extended list activities in this activity group (I was just playing around). If you have different activities, each will have to override onResume() and I guess you'd have to look at the type of context you got back from v.getContext() before casting and calling it.
My ActivityGroup looks something like this:
public class BrowseGroup extends ActivityGroup {
.....
#Override
protected void onResume() {
super.onResume();
// call current activity's onResume()
View v = history.get(history.size()-1);
YFIMenuListActivity currentActivity = (YFIMenuListActivity)v.getContext();
currentActivity.onResume();
}
....
}
I've managed to implement an expanded version of cousin_itt's approach.
In both of my activities being used within the activity group I changed onResume from :
protected void onResume()
to
public void onResume()
I then wrote the following onResume function in my ActivityGroup to manually fire off onResumes:
#Override
protected void onResume() {
super.onResume();
View v = history.get(history.size()-1);
MainPeopleView currentActivity = null;
try {
currentActivity = (MainPeopleView)v.getContext();
currentActivity.onResume();
}
catch ( ClassCastException e ) {
Log.e(TAG, e.toString());
}
ProfileView otherActivity = null;
try {
otherActivity = (ProfileView)v.getContext();
otherActivity.onResume();
}
catch ( ClassCastException e ) {
Log.e(TAG, e.toString());
}
}
I have to say, this feels like the worst android hack I've ever written. I'm never using activitygroup again.
((ReportActivity)getLocalActivityManager().getActivity("ReportActivity")).onResume();
ReportActivity is that name you want to back Activity
ps: v.getContext();
only return the ActivityGroup ,it can't invoke child Activity onResume
I have found that onFocusChanged(boolean hasFocus) is great for situations like ActivityGroup. This will fire, even if onResume() does not. I use it for a few of my apps that have TabHosts and ActivityGroups. Here you can force the refresh and insure that it always gets fired when your Activity regains the focus.
I hope you have write your refresh data code in this method onResume().

Categories

Resources