简体   繁体   中英

How to set Visibility for a float Action menu from another Activity

I have created a float action menu contains float action buttons in an activity, all I need I want to show the same screen in another activity but without this float action menu which is in the main activity I tried to setVisibily(VIEW.Invisible) in the new activity it shows me a null object refrence error how can I solve this

this is the mainActivity which contains the float action menu

public class MenuActivity extends AppCompatActivity {
    FloatingActionButton SplitCheck, HoldItem, VoidItem, ChangeNoOfCovers, Payment;
    public static FloatingActionMenu mainFM;
 @Override
    protected void onCreate(Bundle savedInstanceState) {
            mainFM = (FloatingActionMenu) findViewById(R.id.mainFAB);
        SplitCheck = (FloatingActionButton) findViewById(R.id.splitCheck);
        HoldItem = (FloatingActionButton) findViewById(R.id.holdItem);
        VoidItem = (FloatingActionButton) findViewById(R.id.voidItem);
        ChangeNoOfCovers = (FloatingActionButton) findViewById(R.id.changeNoCover);
        Payment = (FloatingActionButton) findViewById(R.id.paymentID);

        mainFM.setClosedOnTouchOutside(true);
...........................
.................
}
}

this is my second Activity

package abtech.waiteriano.com.waitrer;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.github.clans.fab.FloatingActionMenu;

public class TakeAwayActivity extends AppCompatActivity {

    private FloatingActionButton fabNew;
    private android.support.v7.widget.Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_away);
        fabNew = (FloatingActionButton) findViewById(R.id.newOrderTa);
        final FloatingActionMenu mainFM = (FloatingActionMenu) findViewById(R.id.mainFAB);
        final boolean showButton = getIntent().getBooleanExtra("HIDE_FAB" , false);
        mainFM.setVisibility(showButton ? View.VISIBLE : View.INVISIBLE);

//        MenuActivity.mainFM.setVisibility(View.INVISIBLE);


        fabNew.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent i = new Intent(TakeAwayActivity.this, MenuActivity.class);
                i.putExtra("HIDE_FAB", true);
                startActivity(i);

            }
        });
    }
}

You can not use static variables for those kind of use.

In your specific case, you are trying to access MenuActivity.mainFM before onCreate was execute, so it'll be necessary null.

The good way is to add an extra in your startActivity Intent, then in your onCreate method of your activity, get this extra and show or not your button.

final Intent intent = new Intent(TakeAwayActivity.this, MenuActivity.class);
intent.putExtra(MenuActivity.EXTRA_SHOW_BUTTON_KEY, true);
startActivity(intent);

then in your activity :

public class MenuActivity extends AppCompatActivity {

    public static final String EXTRA_SHOW_BUTTON_KEY = "EXTRA_SHOW_BUTTON_KEY";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout);

        final FloatingActionMenu mainFM = (FloatingActionMenu) findViewById(R.id.mainFAB);
        final boolean showButton = getIntent().getBooleanExtra(EXTRA_SHOW_BUTTON_KEY , false);
        mainFM.setVisibility(showButton ? View.VISIBLE : View.INVISIBLE);

        // ...
    }
}

Because of in your MenuActivity you have to always hide the FloatingActionButton Try this inside MenuActivity onCreate :

   setContentView(R.layout.activity_take_away);
    mainFM = (FloatingActionMenu) findViewById(R.id.mainFAB);
    mainFM.setVisibility(View.INVISIBLE);

I think there is no need for intents because it has to be always hidden

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM