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();
}
Related
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;
}
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);
}
In my app..there are 2 activities i.e.
page1 and page2.
flow is page1-->page2
On page1, i have used many checkboxes. After selection of one checkbox, when i presses the "next" button, it goes to page2 activity showing the result but when i presses back button and select another checkbox, it is still showing the same result on page2 i.e. of previous selection.
I m new to android.please help me out.Thanks in advance.
i thik you should mentain page1 using
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
// here you checkyour checked checkbox items and make it unchecked
}
this mayhelps you
I thought you were having your own back button..if you're using the phone's back button, try this code to finish the current activity and go to the previous activity [this is to be added to page2 activity]:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
If you want to implement your own back button, under the listener for the button, use an intent to start an activity to go back to the previous activity..
Intent intent = new Intent(Page2ActivityFileName.this, Page1ActivityFileName.class);
startActivity(intent);
finish();
See you're probably using startactivity(intent); whereas in this case you should use startactivityforresult(intent); Hope you get it, if not drop a comment I'll elaborate my answer with some code.
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);
}
I use android:windowSoftInputMode="stateVisible|adjustPan" in my manifest file to open the softkeyboard when the main activity is launched.
This works great, apart from when i come back to the main activity from another using the back button; The softkeyboard does not reappear.
How do i make the softkeyboard appear when coming back to the main activity?
Thanks for any help in advance.
On back button it just remove the current activity from the stack and show the previous activity that is why softkeyboard is not getting opened . you can override the onKeyDown() method and on back button you can call your activity again.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start your activity again here
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}