Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6049095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:32:13+00:00 2026-05-23T07:32:13+00:00

I am using onKeyDown for handling the back button but on pressing the back

  • 0

I am using onKeyDown for handling the back button but on pressing the back button the application exits whereas it should go back to the previous activity. Following is the code i’m using:

public class NewsDetails extends Activity{

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent myIntent = getIntent(); 
    String id = myIntent.getStringExtra("content_id");
    String title = myIntent.getStringExtra("title");
    String nTitle = "<font color='white'>"+title+"</font>";
    setContentView(R.layout.web);

    super.onStart();
    final String mimeType = "text/html";
    final String encoding = "utf-8";

    /*Title*/
    WebView wv = (WebView)findViewById(R.id.wv1);
    WebSettings webSettings = wv.getSettings();
    webSettings.setDefaultFontSize(15);
    wv.setBackgroundColor(Color.BLACK);
    wv.loadData(nTitle, mimeType, encoding);

    /*Body*/
    String xml = XMLfunctions.getBodyXML(id);
    String result = xml.replaceAll("<p>", "<p><div align=\"justify\">");
    String nXml = result.replaceAll("</p>", "</div></p>");
    String nBody = "<font color='white'>"+nXml+"</font>" ;
    WebView wv1 = (WebView)findViewById(R.id.wv2);
    wv1.setBackgroundColor(Color.BLACK);
    WebSettings webSettings1 = wv1.getSettings();
    webSettings1.setDefaultFontSize(10);
    wv1.loadData(nBody, mimeType, encoding);

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        Intent intent = new Intent(NewsDetails.this, TopNewsActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        View view = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", intent).getDecorView();
        TopNewsGroup.group.setContentView(view);
        return true;
    }
    return super.onKeyDown(keyCode, event); 

}

public boolean onCreateOptionsMenu(Menu menu) {

    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optionsmenu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()){

        case R.id.refresh:
                startActivity(new Intent(this, NewsDetails.class));
                return true;

        case R.id.search:
                startActivity(new Intent(this, SearchActivity.class));
                return true;

        case R.id.info:
                startActivity(new Intent(this, NewsDetails.class));
                return true;

        case R.id.exit:
                finish();
                return true;
    }
  return false;
}



}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:32:14+00:00Added an answer on May 23, 2026 at 7:32 am

    As i’m using ActivityGroup i wasn’t using the history stack but on using

    public void back() {  
        if(history.size() > 0) {  
            history.remove(history.size()-1);  
            if(history.size()<=0){
                finish();
            }else{
                setContentView(history.get(history.size()-1));
            } 
        }else {  
            finish();  
        }  
    }
    
    @Override  
    public void onBackPressed() {  
        TopNewsGroup.group.back();
    }
    

    i was able to move back to the previous activity.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On pressing the back button, I'd like my application to go into the stopped
I am capturing an image using the following code public class PictureDemo extends Activity
i am using webview in activity and want to use option menu too. but
I'm testing current gps location application using samsung spica 1.5 but it doesn't display
In my application I have only one activity and I am using ViewAnimator for
Using TortoiseSVN against VisualSVN I delete a source file that I should not have
Using VS2008, C#, .Net 2 and Winforms how can I make a regular Button
Using preview 4 of ASP.NET MVC Code like: <%= Html.CheckBox( myCheckBox, Click Here, True,
Using FlashDevelop, I've got a bit of code to detect keyboard events. The problem
I've a question concerning the OnKeyDown event. The OnKeyDown event gives a KeyCode but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.