farid99
farid99

Reputation: 712

Explanation of Fragment lifecycle and how to access it

I am fairly new to Android and am trying to understand how to access - for testing purposes - various components. I have a Settings app that has, for example, "wi-fi", "Bluetooth", "display", and "language." Each of these buttons will open, on the right hand side of the screen, a fragment that displays some more options.

This is a very general question. If I have an activity myActivity, how do I "get" what is being displayed on the screen by the activity at this moment? What is hidden? If the activity has fragment containers, how do I check if any fragments have been initialized? How do I access specific fragments and the values they are displaying? For example, if when clicking "languages" my app displayed a list of checkboxes buttons for the different languages ("Korean", "English", "Spanish",...), starting from my main activity myActivity, how would I get to those strings if I wanted to assert they are being properly displayed?

Upvotes: 1

Views: 211

Answers (1)

The Original Android
The Original Android

Reputation: 6215

You have so many questions, and I am trying to give a good summary for you. I have only two recommendations for you to read up on.

Recommendation 1: Read webpage at Fragments Lifecycle. Code snippet to look at and understand:

FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

Recommendation 2: Read webpage @ Communicating with Other Fragments. The documentation and code samples are good for passing data between Activity and Fragments. Code snippet:

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

Upvotes: 1

Related Questions