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;
}
}
As i’m using ActivityGroup i wasn’t using the history stack but on using
i was able to move back to the previous activity.