简体   繁体   English

如何破坏活动并显示片段?

[英]how to destroy activity and show fragment?

I have a MainActivity which has a navigation drawer and a framelayout container to show different fragments. 我有一个MainActivity,它具有一个导航抽屉和一个用于显示不同片段的framelayout容器。 On my navigation drawer there is an option which launches another activity(lockpattern activity) to show a lockpattern (had to use activity cause the library doesn't support fragments yet. Link to Library ). 在我的导航抽屉中,有一个选项可以启动另一个活动(lockpattern活动)以显示锁模式(必须使用活动,因为库尚不支持片段。请链接到Library )。

Once the user has set up his pattern or canceled the procedure,i want the lockpattern activity to get destroyed and show the previous mainActivity and the same fragment what was there in the container before the lockpattern activity was launched.The problem im facing is that once the backbutton is pressed or even if i call the finish() function,it doesn't show the previous activity(ie Main activity) but instead relaunches the lockpattern activity.i have even tried super.onBackPressed(); 一旦用户设置了其模式或取消了该过程,我就希望lockpattern活动被破坏并显示先前的mainActivity和相同的片段,在启动lockpattern活动之前容器中的内容是什么。我面临的问题是后退按钮被按下,或者即使我调用finish()函数,它也不显示先前的活动(即Main活动),而是重新启动lockpattern活动。我什至尝试了super.onBackPressed(); and it doesn't seem to work.Any help or ideas to get around this is gratefully accepted. 似乎还行不通。我们非常乐意接受任何帮助或解决此问题的想法。

Code of Lockpattern 锁定模式代码

public class Create_Pattern extends Activity {
private static final int REQ_CREATE_PATTERN = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {

    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    // This is your preferred flag
    LockPatternView.MATRIX_WIDTH = 4;
    Intent intent = new Intent(LockPatternActivity.ACTION_CREATE_PATTERN,
            null, getBaseContext(), LockPatternActivity.class);
    startActivityForResult(intent, REQ_CREATE_PATTERN);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
    case REQ_CREATE_PATTERN: {
        if (resultCode == RESULT_OK) {
            char[] pattern = data
                    .getCharArrayExtra(LockPatternActivity.EXTRA_PATTERN);
            DataBaseHandler handler = new DataBaseHandler(this);
            handler.open();

            String PatternToWrite = new String(pattern);
            handler.createPattern(PatternToWrite);
            handler.close();
            Log.d("DEBUG", new String(pattern));

            Toast.makeText(getApplicationContext(), "Pattern Recorded",
                    Toast.LENGTH_LONG).show();
            finish();

        }
        if (resultCode == RESULT_CANCELED) {
            finish();

        }
        break;
    }// REQ_CREATE_PATTERN

    }

}
}

You should create a new Intent that points to the Activity you want to navigate to next, start that activity by calling startActivity(intent). 您应该创建一个新的Intent,该Intent指向您要导航到的活动,然后通过调用startActivity(intent)启动该活动。 Then you can call finish() to destroy the current Activity. 然后,您可以调用finish()销毁当前的Activity。

Another approach, (which I have not tried yet) is to create an 'up button' hierarchy and manually call NavUtils.navigateUpFromSameTask(this); 另一种方法(我尚未尝试过)是创建“向上按钮”层次结构并手动调用NavUtils.navigateUpFromSameTask(this);。

Documentation: 说明文件:

http://developer.android.com/training/implementing-navigation/ancestral.html http://developer.android.com/reference/android/support/v4/app/NavUtils.html http://developer.android.com/training/implementing-navigation/ancestral.html http://developer.android.com/reference/android/support/v4/app/NavUtils.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM