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);
}
Related
When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?
I guess what I am trying to do is to understand the correct behavior I should be employing.
Let's look at some popular browser. currently, in my device, there are two browsers
chrome
default internet browser
In both browsers, it goes to the previous page. So it is a common behavior. to do so
you can overwrite the onBackPressed to go to your previous page.
#Override
public void onBackPressed(){
if(mWebView.canGoBack() == true){
mWebView.goBack();
}
else{
finish();
}
}
Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.
And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().
In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.
You can use this override the onkeydown event and check the key pressed (in this case is back key) and check if is there any previous page
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
When you are clicking back button, you are clicking it for activity that is opening the webview better thing would be to give a back button in you layout..and hence saving the last url opened by user
Something like this in activity with WebView:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
}else{
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
or override onBackPressed method:
#Override
public void onBackPressed() {
if(yourWebView.canGoBack() == true){
yourWebView.goBack();
} else {
finish();
}
}
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); }
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();
}
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 am creating an Android application in my application all Activities (pages) have a Back button so by pressing I can go to previous page or another page.For going on another page I use startActivity() and finish().But when I press keyboard's Back Button it goes to page which was previously pushed in Activities Stack.And I want to synchronize Keyboard's Back button and My Activities Back button.
How can I do this Please Help..
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK)
{
//start youractivity or finish();
}
return super.onKeyDown(keyCode, event);
}
override this method on your activity