开发者

BackButton doesn't call last activity in history stack

I'm having this error with backbutton on my activities. All activities have this problem. When I press Back Button it goes to home screen, not to last activity in stack. I've putted a Log message but in some activities it doesn't print it on LogCat, in this activity posted here it prints the log. But it looks like for some activities is not going inside the method.

There are three tabs in my application, one of them extends ActivityGroup instead of Activity. But I don't think its the problem, I've removed this tab and it keeps happening.

So my question is, how do I solve it, how do I make it go to last activity instead going to home screen?

In fact it goes to the last screen where it was called. If I call it on the home screen it goes to this, if I call it on the main screen, where I put shortcuts and widgets it goes to this screen. And for what I've read in docs, it should go to the last in history stack, which means that this screen is on the stack, but no my activities. Right?

I've tried to put this but doesn't work for all activities. It keeps going to home screen. I don't know what can it be.

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
                finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

I also tried with onBackPressed() method, but nothing. And also, instead of calling finish I tried with a new intent and calling the some activity explicity, but nothing too.

I can't put all my activities here because are a lot.

But Here is one, and maybe if something works with this I can implement on others. Thanks.

public class MyActivity extends Activity {

    private ListView listview;
    private ImageView imgView;
    private Resources res;
    private Drawable transition;
    private SimpleCursorAdapter adapter;
    private DataHandlerDB handler;
    private SQLiteDatabase db;
    private OpenHelper helper;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);          
        helper = new OpenHelper(this);
        db = helper.getWritableDatabase();
        setBasicContent();

    }

    @Override
    public void onStart() {

        super.onStart();
        helper = new OpenHelper(this);
        db = helper.getWritableDatabase();
        setBasicContent();

    }

    @Override
    public void onDestroy() {

        super.onDestroy();
        DataHandlerDB.selectTopCalls(this).close();
        db.close();
        helper.close();

    }

    @Override
    public void onPause() {

        super.onPause();

    }

    @Override
    public void onStop() {

        super.onStop();
        DataHandlerDB.selectTopCalls(this).close();
        db.close();
        helper.close();

    }

    @Override
    protected void onResume() {

        super.onResume();
        setBasicContent();

    }


    public void setBasicContent() {

        listview = (ListView) findViewById(R.id.list_view);

        Cursor c = DataHandlerDB.selectTopCalls(this);      

        c.moveToFirst();

        startManagingCursor(c);     

        adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
                new String[] { DataHandlerDB.CONTACT_NAME_COL,
                        DataHandlerDB.CONTACT_NUMBER_COL,
                        DataHandlerDB.CONTACT_DURATION_COL,
 开发者_Python百科                       DataHandlerDB.CONTACT_DATE_COL }, new int[] {
                        R.id.contact_name, R.id.phone_number, R.id.duration,
                        R.id.date });       

        listview.setAdapter(adapter);
    }
}

Here is the activity that extends ActivityGroup its called like this:

tabHost.addTab(tabHost.newTabSpec("three").setIndicator("Filter Options", 
                res.getDrawable(R.drawable.filteroptionsiconfile))
                .setContent(new Intent(this, FilterOptionsGroup.class)));

FilterOptionsGroup.java

public class FilterOptionsGroup extends ActivityGroup {

    public static FilterOptionsGroup group;
    private ArrayList<View> history;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.history = new ArrayList<View>();
        group = this;

        View view = getLocalActivityManager().startActivity(
                "FilterOptions",
                new Intent(this, FilterOptions.class)
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
                .getDecorView();

        replaceView(view);

    }

    public void replaceView(View v) {
        // Adds the old one to history
        history.add(v);         
        // Changes this Groups View to the new View.
        setContentView(v);
    }

}


If all of your activities are encapsulated within a tab host and they are all hosted in tabs then they are not really sitting in the stack. They are sitting within the foreground view which is the tab host screen and the back button will drop you to the previous app.


Is the activity that is calling this activity perhaps finishing itself? If you don't override onkeydown, the default behavior is to handle the back button. Note that you're confusing backButtonPressed with onBackPressed. Use the @Override annotation to ensure that you're overriding the right methods...

From the android source for Activity

public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        if (getApplicationInfo().targetSdkVersion
                >= Build.VERSION_CODES.ECLAIR) {
            event.startTracking();
        } else {
            onBackPressed();
        }
        return true;
    }
<snip>

......

So, you really shouldn't even need to do anything.


Check your manifest , you may have set the android:noHistory to true


try to call super.onKeyDown(keyCode, event); in the beginning of the onKeyDown method. I had similar problem and it was a solution

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜