2

I have a FrameLayout and put there some fragments by click on button, next click should remove fragment from FrameLayout, I do this by removeAllViews() (FrameLayout is in another Fragment so translaction method is in Activity). I need to do some action when removeAllViews() starts and have to do it in Fragment class but something goes wrong.

I tried: OnDestroy() OnDestroyView() OnPause() in Fragment class

but it works like:

  1. put Fragment in FrameLayout (from Activity)
  2. use removeAllViews() (from Activity)
  3. there is no Fragment in FrameLayout (is clear) but nothing else happens and methods are not working
  4. put new Fragment in FrameLayout (from Activity) - now all methods (OnDestroy() from Fragment class) works (probably it's real time to destroy old fragment)

How is it possible to 'get moment' when Fragment is not exists for user? I want to send some information to server if user hides Fragment.

@Edit3 code from method from Activity where I want to make translaction

public void showProductsList(String productType,int containerID){

        List<String> prodNames = new ArrayList<String>();
        List<Long> prodIds = new ArrayList<Long>();

            DatabaseDAOProdProtein dao = new DatabaseDAOProdProtein(getApplicationContext());
            dao.open();
            List<DatabaseProduct> productList = dao.getAllProducts();
            for(int i=0;i<productList.size();i++){
                prodNames.add(productList.get(i).getName());
                prodIds.add(productList.get(i).getId());
            }
            dao.close();

        ProductsList productsList = new ProductsList(productType,prodNames,prodIds);

        productsList.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // Note that system bars will only be "visible" if none of the
                        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                        Toast.makeText(getApplicationContext(),"action1 " ,Toast.LENGTH_LONG).show();
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            // TODO: The system bars are visible. Make any desired
                            // adjustments to your UI, such as showing the action bar or
                            // other navigational controls.
                            Toast.makeText(getApplicationContext(),"action2 " ,Toast.LENGTH_LONG).show();
                        } else {
                            // TODO: The system bars are NOT visible. Make any desired
                            // adjustments to your UI, such as hiding the action bar or
                            // other navigational controls.
                            Toast.makeText(getApplicationContext(),"action3 " ,Toast.LENGTH_LONG).show();
                        }
                    }
                });

        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(containerID, productsList).commit();
    }

I used this method in another Fragment by:

((MainActivity) getContext()).showProductsList("carb", carbContainer.getId());

there is an error:

Error:(560, 21) error: cannot find symbol method setOnSystemUiVisibilityChangeListener(<anonymous OnSystemUiVisibilityChangeListener>)

barmi
  • 665
  • 6
  • 22

1 Answers1

1

You say:

"How is it possible to 'get moment' when Fragment is not exists for user? I want to send some information to server if user hides Fragment."

I now know you did not mean "hide", so just use the OnDestroy() method.

Try this to trigger the "hide"

View topLevelLayout = findViewById(R.id.top_layout);
topLevelLayout.setVisibility(View.INVISIBLE);

You cannot go into stopped state while Fragment (Activity) is visible. Android destroying activities, killing processes

The best way to make sure something runs via a view is to run it via a post:

topLevelLayout.post(new Runnable()
{
@Override
public void run()
{
        topLevelLayout.removeAllViews();
    }
}

To get notified of system UI visibility changes, register an View.OnSystemUiVisibilityChangeListener to your view (fragment).

https://developer.android.com/training/system-ui/visibility.html

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Toast.makeText(getContext(),"action0 " ,Toast.LENGTH_LONG).show();
    Fragment your_frag = new ProductsList(productType,prodNames,prodIds);
getSupportFragmentManager().beginTransaction().replace(containerID,your_frag).commit();
    getSupportFragmentManager().executePendingTransactions();//make sure onCreateView has executed
        your_frag.getRootView().setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // Note that system bars will only be "visible" if none of the
                        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                        Toast.makeText(getContext(),"action1 " ,Toast.LENGTH_LONG).show();
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            // TODO: The system bars are visible. Make any desired
                            // adjustments to your UI, such as showing the action bar or
                            // other navigational controls.
                            Toast.makeText(getContext(),"action2 " ,Toast.LENGTH_LONG).show();
                        } else {
                            // TODO: The system bars are NOT visible. Make any desired
                            // adjustments to your UI, such as hiding the action bar or
                            // other navigational controls.
                            Toast.makeText(getContext(),"action3 " ,Toast.LENGTH_LONG).show();
                        }
                    }
                });
    }    

A typical fragment looks like this:

public class HomeFragment extends Fragment {
    View                        mRootView     = null;
    public HomeFragment(){}//null constructor

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        mRootView = inflater.inflate(R.layout.fragment_home, container, false);

        return mRootView ;
    }
public View getRootView ()
{
    return mRootView;
}
}
Community
  • 1
  • 1
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
  • Put the listener on your fragment when you create it (the rootView). – Jon Goodwin Nov 05 '16 at 23:32
  • I create it in Activity (edited my post again). Could you explain me how to put listener please? – barmi Nov 05 '16 at 23:40
  • Is this (the code I've added) making sense to you ? – Jon Goodwin Nov 06 '16 at 00:20
  • Is there any way to use this method outside the `OnCreate()`? (I dont want to load fragment when activity is started, just by `((MainActivity) getContext()).showProductsList("protein", proteinContainer.getId());` in another fragment) – barmi Nov 06 '16 at 00:21
  • Yes you can do it where you want. – Jon Goodwin Nov 06 '16 at 00:22
  • So should I put `setOnSystemUiVisibilityChangeListener` in method in Activity where I want to make translaction? edited my post for current code – barmi Nov 06 '16 at 00:25
  • Once you create the fragment (where ever you want) attach the Listener to it's rootView, then execute the transaction on the fragment. – Jon Goodwin Nov 06 '16 at 00:28
  • Yes you need to use the rootView (look at the code I commented out), add the getRootView method to your fragment, OnSystemUiVisibilityChangeListener operateds on a View. – Jon Goodwin Nov 06 '16 at 00:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/127458/discussion-between-jon-goodwin-and-barmi). – Jon Goodwin Nov 06 '16 at 00:38