Reputation: 19
i am getting a strange problem.
On pressing the back key activity is not going back. in logcat it is showing
04-15 19:24:09.063: V/Activity(23041): Tracking Key Up, activity is resumed: false
here is my code for the activity
package com.sandy.letsfixthat;
import android.app.Activity;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class Slider extends Activity {
TabSpec beginer,inter,dev;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.slider);
TabHost th = (TabHost)findViewById(R.id.tabhost);
LocalActivityManager mLocalActivityManager = new LocalActivityManager(this, false);
// state will be bundle your activity state which you
// get in onCreate
mLocalActivityManager.dispatchCreate(savedInstanceState);
th.setup(mLocalActivityManager);
//tab for beginner
beginer = th.newTabSpec("Begin");
beginer.setIndicator("Beginner");
Intent begin = new Intent(this, Beginner.class);
beginer.setContent(begin);
th.addTab(beginer);
//tab for intermediate
inter = th.newTabSpec("Inter");
inter.setIndicator("Intermediate");
Intent Intermediate = new Intent(this, Intermediate.class);
inter.setContent(Intermediate);
th.addTab(inter);
//tab for developers
dev = th.newTabSpec("Devel");
dev.setIndicator("Other");
Intent develop = new Intent(this, Developer.class);
dev.setContent(develop);
th.addTab(dev);
}
public boolean onCreateOptionsMenu(android.view.Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()){
case R.id.aboutUs:
Intent i = new Intent(this,About.class);
startActivity(i);
break;
case R.id.exit:
finish();
break;
/* case R.id.setting:
Intent setting = new Intent (this,Settings.class);
startActivity(setting);
break; */
}
return false;
}
@Override
public void onPause() {
// your code.
super.onPause();
finish();
}
}
code of MainActivity
package com.sandy.letsfixthat;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
Button go;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
go = (Button)findViewById(R.id.bgo);
go.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()){
case R.id.bgo:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("NOTE:- this is an initial release. MORE TUTORIAL COMING IN NEXT UPDATE")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do things
Intent i = new Intent(MainActivity.this,Slider.class);
startActivity(i);
}
});
AlertDialog alert = builder.create();
alert.show();
break;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
When I open this slider activity and press back button, nothing happens. What it should do on back button press is EXIT THE APP.
Upvotes: 2
Views: 354
Reputation: 10063
Normally, you shouldn't need to do anything for the back button to terminate your activity. What you need to do is not override onBackButtonPressed() or onKeyDown(). Or if you do override onKeyDown(), then don't do anything for keycode KEYCODE_BACK other than return false.
Note that this only terminates the activity, not the application. If you want to exit the entire application, then it's a little trickier. Let us know if that's what you wanted to do, and I'll expand on this answer.
Upvotes: 0
Reputation: 21
just add this Line:
mLocalActivityManager.dispatchResume();
worked for me!
Upvotes: 2
Reputation: 31
You can override the back button function with:
public void onBackPressed(){
finish();
}
so it will finish your app. Just keep in mind that this is not the best solution and you should not use finish () in the on pause method.
Upvotes: 0