Fragment Will Not Switch Back View and Boolean Variable is Showing as Always False

Timmo

I have an application with two fragments which will not switch back to the original from the method I am using. I have a feeling this is not the way to carry out this. Where am I going wrong? My code is below.

// region GlobalVars
boolean viewLib;
// endregion

// region Init
void Init () {
    viewLib = false;
}
// endregion

// region onCreate
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_music);
    Init();

    // Set Initial Library Fragment
    if (findViewById(R.id.fragment_container) != null) {
        if (savedInstanceState != null) {
            return;
        }
        PlayerFragment firstFragment = new PlayerFragment();
        firstFragment.setArguments(getIntent().getExtras());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, firstFragment).commit();
    }
}
// endregion

// region SwitchView
void SwitchView () {
    if (viewLib = false) {
        Toast.makeText(getApplicationContext(), "viewLib", Toast.LENGTH_SHORT).show();
        Library();
        viewLib = true;
    } else {
        Toast.makeText(getApplicationContext(), "!viewLib", Toast.LENGTH_SHORT).show();
        Player();
        viewLib = false;
    }
}
//region Library
void Library() {
    LibraryFragment newFragment = new LibraryFragment();
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    transaction.commit();
}
// endregion

//region Player
void Player() {
    PlayerFragment newFragment = new PlayerFragment();
    //Bundle args = new Bundle();
    //args.putInt(LibraryFragment.ARG_POSITION, position);
    //newFragment.setArguments(args);
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    transaction.replace(R.id.fragment_container, newFragment);
    transaction.addToBackStack(null);

    transaction.commit();
}
// endregion
// endregion

// region Menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_music, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_library:
            SwitchView();
            return true;
        case R.id.action_settings:
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
// endregion
Timmo

Fixed My problem. I did this:

void SwitchView() {
    if (!viewLib) {
        Toast.makeText(getApplicationContext(), "viewLib", Toast.LENGTH_SHORT).show();
        Library();
        viewLib = true;
    } else {
        Toast.makeText(getApplicationContext(), "!viewLib", Toast.LENGTH_SHORT).show();
        Player();
        viewLib = false;
    }
}

Seems to work fine now after fixing the mistake in the boolean test. Now all I have to do is override onBackPressed to stop the constant views

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Boolean condition is always false when using `status == true`

From Dev

Is a boolean instance variable default value true or false

From Dev

Showing a fragment with different parameters in tabs of a view pager

From Dev

Why is isDrawerVisible always showing as false

From Dev

Boolean always returns false

From Dev

How to pass boolean variable of false in a URL

From Dev

Gson boolean conversion always false

From Dev

Variable always evaluates to false

From Dev

Function returns always Boolean False

From Dev

SharedPreferences variable always returns false

From Dev

Is a boolean instance variable default value true or false

From Dev

iAd always showing at bottom of view controller

From Dev

Showing grid view inside a dialog from a fragment

From Dev

Why is isDrawerVisible always showing as false

From Dev

How to go switch back to the MainActivity from a fragment?

From Dev

Variable always evaluates to false

From Dev

shared preference Boolean is always returning false

From Dev

fragment.isVisible() always returns false

From Dev

Why List View is not showing in the fragment?

From Dev

Solr 6.2.0 sending boolean values as false always

From Dev

Python - Compare number with boolean (always return false)

From Dev

Javascript object's boolean variable always false

From Dev

switch line class in clojurescript with if boolean variable

From Dev

Boolean values return false always

From Dev

RecyclerView not showing items inside view pager fragment

From Dev

Swift - Animation not working when switch view and back

From Dev

Always returning false as boolean value

From Dev

Fragment not showing Recycler View

From Dev

back button press for view in fragment

Related Related

HotTag

Archive