单击活动中的按钮可同时打开两个活动

时间:2018-01-26 14:17:52

标签: java android android-layout android-intent

这里我有三个activities,其中一个是login activity。每当我点击login button两个活动同时出现时。另外两个activities是:VendorAccount.javaPromotion.java。代码如下:

我在intent使用了logging in,只要Promotion.class的任务成功启动public class Login extends AppCompatActivity { private static final String TAG = "Login"; private FirebaseAuth mAuth; private Button loginButton,signupButton; EditText eml,pass; String email,password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); mAuth = FirebaseAuth.getInstance(); loginButton=(Button)findViewById(R.id.btn_login); signupButton=(Button)findViewById(R.id.newuser); loginButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { eml=findViewById(R.id.edt_email); pass=findViewById(R.id.edt_password); email=eml.getText().toString(); password=pass.getText().toString(); // startActivity(new Intent(Login.this,Promotion.class)); mAuth.signInWithEmailAndPassword(email,password) .addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()) { // Sign in success, update UI with the signed-in user's information Log.d(TAG, "signInWithEmail:success"); startActivity(new Intent(Login.this,Promotion.class)); FirebaseUser user = mAuth.getCurrentUser(); //updateUI(user); } else { // If sign in fails, display a message to the user. Log.w(TAG, "signInWithEmail:failure", task.getException()); Toast.makeText(Login.this, "Authentication failed.", Toast.LENGTH_SHORT).show(); //updateUI(null); } // ... } }); } }); signupButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(Login.this,Signup.class)); } }); } @Override public void onStart() { super.onStart(); // Check if user is signed in (non-null) and update UI accordingly. FirebaseUser currentUser = mAuth.getCurrentUser(); //updateUI(currentUser); } }

Login.java

signin

现在当我点击Promotion.class按钮时,它应该会打开VendorAccount.class,但它也会打开public class Promotion extends AppCompatActivity { private BottomBar bottomBar; private Button coupons,scanqr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.promotion); Toast.makeText(getApplicationContext(), "Promotion called",Toast.LENGTH_LONG).show(); coupons=(Button)findViewById(R.id.coupons); scanqr=(Button)findViewById(R.id.scanqr); bottomBar=BottomBar.attach(this,savedInstanceState); bottomBar.setItems(R.menu.bottombars_menu); bottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() { @Override public void onMenuTabSelected(int menuItemId) { if (menuItemId==R.id.tab_account){ //Toast.makeText(getApplicationContext(),"Accounts",Toast.LENGTH_SHORT).show(); startActivity(new Intent(Promotion.this,VendorAccount.class)); } else if (menuItemId==R.id.tab_details){ //Toast.makeText(getApplicationContext(),"Customer Details",Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(),ShowCustomer.class)); } else if (menuItemId==R.id.tab_coupons) { //Toast.makeText(getApplicationContext(),"Coupons",Toast.LENGTH_SHORT).show(); startActivity(new Intent(Promotion.this,ShowCoupons.class)); } } @Override public void onMenuTabReSelected(int menuItemId) { } }); // Setting colors for different tabs when there's more than three of them. // You can set colors for tabs in three different ways as shown below. bottomBar.mapColorForTab(0, ContextCompat.getColor(this, R.color.colorAccent)); bottomBar.mapColorForTab(1, 0xFF5D4037); bottomBar.mapColorForTab(2, "#7B1FA2"); coupons.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(Promotion.this,Coupons.class)); } }); scanqr.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(),"QR scan",Toast.LENGTH_SHORT).show(); configure_button(); } }); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); // Necessary to restore the BottomBar's state, otherwise we would // lose the current tab on orientation change. bottomBar.onSaveInstanceState(outState); } private void configure_button() { if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED ) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { requestPermissions(new String[]{android.Manifest.permission.CAMERA} , 0); } return; } if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED ){ startActivity(new Intent(Promotion.this,QRScan.class)); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if(requestCode == 0){ if(grantResults.length>0 && grantResults[0]==PackageManager.PERMISSION_GRANTED ){ new Thread(new Runnable() { @Override public void run() { configure_button(); startActivity(new Intent(Promotion.this,QRScan.class)); } }).start(); }else{ Toast.makeText(getApplicationContext(), "Access Denied ! Plesae Choose Camera Access Manually ", Toast.LENGTH_SHORT).show(); } } } @Override public void onBackPressed() { AlertDialog alertDialog = new AlertDialog.Builder(Promotion.this).create(); alertDialog.setTitle("System Message!!"); alertDialog.setMessage("Hey There,!!"+"\n"+"Do Tou Really Want to Leave?"); alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }); alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); alertDialog.show(); } }

Promotion.java

public class VendorAccount extends AppCompatActivity {

private ImageView displayPic;
private EditText name,addr1,addr2,phno;
private Button save;
private Button image1,image2,image3,image4;
String vendorname,vendoraddr1,vendoraddr2,vendorphno;

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

    Toast.makeText(getApplicationContext(),
            "VendorAccount called",Toast.LENGTH_LONG).show();

    displayPic=(ImageView)findViewById(R.id.displaypicture);
    image1=(Button) findViewById(R.id.image1);
    image2=(Button) findViewById(R.id.image2);
    image3=(Button) findViewById(R.id.image3);
    image4=(Button) findViewById(R.id.image4);
    save=(Button)findViewById(R.id.saveButton);
    name=(EditText)findViewById(R.id.vendorname);
    addr1=(EditText)findViewById(R.id.vendoradd1);
    addr2=(EditText)findViewById(R.id.vendoradd2);
    phno=(EditText)findViewById(R.id.vendorphno);

    displayPic.setOnClickListener(choosePic);
    image1.setOnClickListener(choosePic);
    image2.setOnClickListener(choosePic);
    image3.setOnClickListener(choosePic);
    image4.setOnClickListener(choosePic);

    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getApplicationContext(),"Saved in database",Toast.LENGTH_SHORT).show();
            vendorname=name.getText().toString();
            vendoraddr1=addr1.getText().toString();
            vendoraddr2=addr2.getText().toString();
            vendorphno=phno.getText().toString();
        }
    });
}

public View.OnClickListener choosePic=new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent i = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        final int ACTIVITY_SELECT_IMAGE = 1234;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
    }
};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch(requestCode) {
        case 1234:
            if(resultCode == RESULT_OK){
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = cursor.getString(columnIndex);
                cursor.close();
                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
            }
        }
}
}

VendorAccount.java

Toast

正如您所看到的,我已在每个类的onCreate方法中添加了class条消息,以便我知道调用了哪个signin。因此,当我点击Toast按钮时,会显示Promotion.java的{​​{1}}消息,然后显示Toast的{​​{1}}消息和VendorAccount.java的布局首先显示。

有人可以帮我解决这个问题吗?

VendorAccount.javamanifest文件位于以下位置:

的AndroidManifest.xml

activiy_login

activity_login.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vendorapp">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".Coupons"
        android:theme="@style/AppThemeNoBar" />
    <activity
        android:name=".Promotion"
        android:label="Promotion"
        android:theme="@style/AppThemeNoBar" />
    <activity
        android:name=".Login"
        android:label="Login"
        android:theme="@style/AppThemeNoBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Signup"
        android:label="SignUp"
        android:theme="@style/AppThemeNoBar" />
    <activity
        android:name=".CouponsNew"
        android:theme="@style/AppThemeNoBar" />
    <activity
        android:name=".QRScan"
        android:theme="@style/AppThemeNoBar" />
    <activity android:name=".ShowCoupons"></activity>
    <activity android:name=".qrinfo"></activity>
    <activity
        android:name=".ShowCustomer"
        android:theme="@style/AppThemeNoBar" />
    <activity
        android:name=".VendorAccount"
        android:theme="@style/AppThemeNoBar">

    </activity>
</application>

2 个答案:

答案 0 :(得分:0)

您是否正在使用BottikeBar库? setOnMenuTabClickListener()是版本1.3.9的方法,但最新版本为2.3.1。 API发生了很大变化。我想知道该版本是否存在导致setOnMenuTabClickListener()在初始设置中被调用的错误(默认选择第一个选项卡是正确的?)这可以解释为什么Promotion启动时,它会立即启动VendorAccount。尝试升级到2.3.1,文档为here

答案 1 :(得分:0)

在onCreate期间初始化onMenuTabSelected时,您的Promotiion.class会被触发。

尝试设置int变量以覆盖第一个onMenuTabSelected

public class Promotion extends AppCompatActivity {
private int itemSelectedCounter; 
bottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
    @Override
    public void onMenuTabSelected(int menuItemId) {
       if(++itemSelectedCounter > 1) {
          // your onMenuTabSelectedCode
       }
    }