提问者:小点点

在静态编程语言中关闭第二个Activity时,从片段中清除主Activity


当我关闭第二个活动时,我试图从片段中清除我的主要内容。
我以这种方式调用片段以通过FrameLayout显示在主活动上

override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        R.id.action_about -> {
            load_about(loaderAboutFragment = AboutFragment())
            Toast.makeText(applicationContext,"About the Author", Toast.LENGTH_LONG).show()
        }
        R.id.action_affiliate -> {
            load_affiliate(loaderAffiliateFragment = AffiliateFragment())
            Toast.makeText(applicationContext,"Affiliate Disclosure", Toast.LENGTH_LONG).show()
        }
        R.id.action_terms -> {
            load_terms(loaderTermsFragment = TermsFragment())
            Toast.makeText(applicationContext,"Terms And Conditions of Use", Toast.LENGTH_LONG).show()
        }

        else -> return super.onOptionsItemSelected(item)
    }
    return true
}

// For  handling layout / opening Fragment
private fun load_about(loaderAboutFragment: AboutFragment) {
    val fm = supportFragmentManager.beginTransaction()
    fm.replace(R.id.frameLayout, loaderAboutFragment)
    fm.addToBackStack(null)
    fm.commit()
}

这是第二次活动

class CentersActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_centers)

        //Action Bar Button / Back to Home
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)

        //Listview of Centers
        val regioncenters = resources.getStringArray(R.array.centers_list)
        var lv = findViewById<ListView>(R.id.center_content_lview)
        val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, regioncenters)
        lv.adapter = adapter
    }

    //Listener for Back to Home Button
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when (item.getItemId()) {
            android.R.id.home -> {
                finish()
                setContentView(R.layout.activity_main)
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
}

我该怎么做?我应该在我的主页按钮侦听器上放什么。


共1个答案

匿名用户

在MainActivity中,转到您打开第二活动的意图。而不是

startActivity(intent);

使用

startActivityForResult(intent, REQUEST_CODE); //where req code is a static final integer.

在完成()之前的秒活动中写:

setResult(RESULT_OK);

现在回到MainActivity。覆盖此方法

   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                //this is how you remove one fragment. You might want to extract this in a method (select lines and ctrl+alt+M),and call the method here for each of your fragments' ids. 
                val fm = fragmentManager().findFragmentById(R.id.fragmentName);
                if(fm != null) //this ensures that if a fragment is not open, it will not be closed. Thus avoiding an error
                    supportFragmentManager()
                        .beginTransaction()
                        .remove(fm)
                        .commit();
            }
        }
    }