Android Back button with Fragment Activity? - android

Hi guys i am developing an application with Extending fragment activity,here is my problem when i have used the normal activity is i have used below method.
onKeyDown(KeyEvent.KEYCODE_BACK, newKeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_BACK));
For activity what i need to do?
How can i achieve this goal

I found this link to help. Sorry that its old, but it kept coming up on the google.
Fragment: which callback invoked when press back button & customize it
Setup the callback in the activity. Then the fragment doesnt need an #Overrride.
note this will work for the back button, as well as onKeyDown.

I've implemented onBackPressed in my (Fragment)Activity and simply forwarded an appropriate message to the relevant fragment.
Furthermore can't you implement your onKeyDown event in your parent activity and do what you need to do which may be passing messages to your fragments?

Hi Please try below code.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent i1 = new Intent(Activity1.class, Activity2.class);
startActivity(i1);
return false;
}
return super.onKeyDown(keyCode, event);
}

Related

Navigating between activities

I'm developing an app. for my semester project.App. is simple true&false game.I did something about it but i choked up one point.My problem is that handling activities.
Here is the quick view of the design.
http://imgur.com/2Fhsrn8
I created all activities and my codes working correct.But there is an issue.When i'm on third or fourth page(activity) and press back button on my phone it returns the second page then i automatically have another chance to answer question.All i want is turn the first screen when back button is pressed and clear all the data such as assign score as 0.
I'will be grateful for any kind of help.
Use
finish();
for your current activity when you start the new intent(e.g. when going from activity 3 to activity on your picture) and add this to your activity's 2-3 if you want to start again when you click back, otherwise it will close your app.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
//implement code to start 1st activity here!
}
return super.onKeyDown(keyCode, event);
}
you should override onBackPressed on your third, and fourth activity ->
#Override
public void onBackPressed() {
// here you should create intent to your firstActivity
// and assign variables -> e.g.
// score = 0;
}

How to dispatchKeyEvent to currentView in TabHost

I need to change the KeyEvent dispatch in TabActivity. If the current tab content activity/view can handled the KeyEvent.KEYCODE_Back, let it handled it. If not, show dialog to tip like this:"Would you want exit?". How can do this?
I have tried by this code in my TabActivity:(can't implement my demand)
#Override
public boolean dispatchKeyEvent(Event event){
if(event.getKeyCode() == KeyEvent.KEYCODE_BACK){
if(!mTabHost.getCurrentView.dispatchKeyEvent(event)){
showDialog(0);
return true;
}
}
return super.dispatchKeyEvent(event);
}
I think this link can make you clear.
you should override onKeyDown method, and directly call mTabHost.onKeyDown(...) method, check it return result, if false, it means you should show the exit message to user.
hope can help you.

Android webview exiting app on backbutton after loadData

I have an application which loads data via Activity --> onStart() --> new LoadTask through AsyncTask --> onPosExecute() --> ListView --> listView.setOnItemClickListener() --> onItemClick() -->Creates Webview
After clicking on the listView, the WebView is invoked to show further details but when you click to go back from the WebView to the listView instead the application exits. Any help would be greatly appreciated on this.
I think you use this code in onItemClick() finish() to finish your activity Remove this(finish();) code when you call the Intent to open the webView
A hacky way around this admittedly odd behaviour would be overwriting the back button. It's generally not accepted when making the app perform not-as-the-user-expects-it-to, but in your case it might be of some help.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
startActivity(new Intent(myClass.this, ListViewClass.class));
return true;
}
return super.onKeyDown(keyCode, event);
}

Navigation inside tab in android

I have a tab activity, which contains 4 tabs and each one contain activities. In the fourth tab i have got a list activity. it will list a number of options. when we click and option it will go to another activity. and when i press back button from that activity the application exits. But actually i want to go back to the list of options.
Can any body help me to get rid of this
Regards
Pramod
Override the back button with this code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//Enter code here telling it what you want to do when you hit back
return true;
}
return super.onKeyDown(keyCode, event);
}
Hope that helps!!
Override the onPause() of the Activity you mentioned above "when we click and option it will go to another activity" to go to the Activity of the Tab containing the options via Intent with its class.
Use getParent() to the activity & use the below to so that you will be able add up view by clearing up existing views & adding new views to the top.
Window window = getLocalActivityManager().startActivity(Id,intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
All the tabs reside inside the activity, so when the backkey has been pressed the complete activity goes into background, so if you want to navigate to a particular activity override the onkeydown method and navigate to the particular tab
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
MyActivity mMyActivity;
mMyActivity = (MyActivity) this.getParent();
mMyActivity.switchTab(tabnumber);
return true;
}
return super.onKeyDown(keyCode, event); }

Android Back Button Utilization Inside ActivityGroup

I am developing an application using TabHost. I am using android default back button to move back to previous activity from current activity by overriding onBackPressed() method inside ActivityGroup of each tab.
Now , the problem is , in one of my activity i have an EditText which get focused when the activity starts. Then , if i press back , it does not go to previous activity , instead it closes the application. By searching about the problem on internet what i have found is that when the EditText get focused which is a child view of activity's view , the activity loss focus and then if back button is pressed , for lack of focus on the current activity it closes the application. Still i am little bit confused or can be say not clear about the problem.
So, any how , i have managed to set and remove focus over EditText on run time using code. But still now , as the EditText does not have focus , if the back button is pressed , it closes the application. I am really confused what's actually going on. So , if anyone have any idea or solution about the problem , please help on the issue. I will appreciate that very much. Thanks.
You can override this behavior by adding Key Listener to your EditText. Try this out,
name_edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.i("Back event Trigered", "Back event");
activitygroup.back();
}
return false;
}
});
try this..
#Override
public void onBackPressed() {
onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
super.onBackPressed();
}
try this
public void onBackPressed() {
startActivity(new Intent(currentActivity.this, previousActivity.class));
finish();
}

Categories

Resources