1

I am trying to clear my main content from fragments when I close second activity.
I call fragments this way to display on the main activity via 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()
}

This is the Second Activity

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)
    }
}

How do I do that? What should I put on my Home Button Listener.

Shivam Kumar
  • 1,892
  • 2
  • 21
  • 33
  • `finish(); setContentView(R.layout.activity_main)` what are you trying to do here? – Tim Aug 23 '18 at 07:24
  • I am trying to set my main activity content to default where fragments are not loaded.That is where the back button going to main activity. How do I do that? –  Aug 23 '18 at 07:32

1 Answers1

1

In MainActivity go to the Intent where you opened SecondActivity. Instead of

startActivity(intent);

use

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

In SecondActivity before finish() write:

setResult(RESULT_OK);

Now go back to MainActivity. Override this method

   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();
            }
        }
    }
Alex Sicoe
  • 140
  • 2
  • 10
  • I am sorry I am new to programming. What should I declare on REQUEST_CODE? And what is the code to close Fragments? –  Aug 23 '18 at 11:44
  • the request code can be any number of your choice. So you can put startActivityForResult(intent, 1); and it will be fine. If you ever need to use startActivityForResult in the same parent activity again, but for another purpose, just be careful to assign a different request code than what is already used. Now, in order to organize your code a little, and remain DRY (=don't repeat yourself) you replace that 1 with a constant, and put this as a field of the class. Like this https://stackoverflow.com/questions/40352879/what-is-the-equivalent-of-java-static-final-fields-in-kotlin – Alex Sicoe Aug 23 '18 at 11:54
  • Still doesn't answer my question how to close or reset the the fragment. Or maybe I dont understand. –  Aug 23 '18 at 12:23
  • sorry, i was running out of characters. Just updated my main post. Check if it works – Alex Sicoe Aug 23 '18 at 12:25
  • I am sorry I am confused about the explanation. I don't understand how the request code change. I am getting in supportFragmentManager and R.id.fragmentName –  Aug 23 '18 at 13:16