Sevket Balik
Sevket Balik

Reputation: 23

get Extra from different Intents

I have an MainActivity which has different Buttons which opens the same PopUpActivty. I am sending the tag with an Intent Extra from each Button in a Method. This Extra should help me to know which button is clicked in the MainActivity. Whit this information I want to open with one button in the PopupWindow diffrent Avtivities, depens on which button is on the MainActvity clicked.

My Problem:

I used getIntent in the PopUpWindow to get the Extra, but the Problem is, I am getting all tags from each buttons instead of just the clicked button.

 public void startPopUpActivity (View view) {

        String tag = view.getTag().toString();

        Intent startKarteikartenActivity = new Intent (this, popUpWindow.class );
        startKarteikartenActivity.putExtra("Activity", tag);
        startActivity(startKarteikartenActivity);
    }

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






public class popUpWindow extends Activity {

    public void startActicity (View view) {
        Intent getIntent = getIntent();

        String activity = getIntent.getStringExtra("Activity");

        switch  (activity){

            case "1":
                Intent intentKarteikarte = new Intent(getApplicationContext(), Karteikarten.class);
                startActivity(intentKarteikarte);
            case "2":
                Intent intentAusbildungsmethoden = new Intent(getApplicationContext(), Ausbildungsmethoden.class);
                startActivity(intentAusbildungsmethoden);
            case "3":
                Intent intentFallbearbeitung = new Intent(getApplicationContext(), Fallbearbeitung.class);
                startActivity(intentFallbearbeitung);
            case "4":
                Intent intentBonus = new Intent(getApplicationContext(), Bonus.class);
                startActivity(intentBonus);
            case "5":
                Intent intentGeneralprobe = new Intent(getApplicationContext(), Generalprobe.class);
                startActivity(intentGeneralprobe);

        }

    }

    public void openPremiumversion (View view) {

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
        startActivity(browserIntent);

    }

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

        DisplayMetrics displayMetrics = new DisplayMetrics();

        getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;

        getWindow().setLayout((int) (width * 0.8), (int) (height * 0.5));
    }
}`

Upvotes: 2

Views: 52

Answers (2)

Amit Gupta
Amit Gupta

Reputation: 663

You are missing the break in your switch case,Also you could use single Intent variable

public void startActicity (View view) {
        Intent getIntent = getIntent();

        String activity = getIntent.getStringExtra("Activity");

        Intent intent;
        switch  (activity){

            case "1":
                intent = new Intent(getApplicationContext(), Karteikarten.class);
                break;
            case "2":
                intent = new Intent(getApplicationContext(), Ausbildungsmethoden.class);
                break;
            case "3":
                intent = new Intent(getApplicationContext(), Fallbearbeitung.class);
                break;
            case "4":
                intent = new Intent(getApplicationContext(), Bonus.class);
                break;
            case "5":
                intent = new Intent(getApplicationContext(), Generalprobe.class);
                break;

        }
       startActivity(intent);

    }

Upvotes: 0

Công Hải
Công Hải

Reputation: 5241

Your method startActvity you forgot add break in switch

      switch  (activity){

            case "1":
                Intent intentKarteikarte = new Intent(getApplicationContext(), Karteikarten.class);
                startActivity(intentKarteikarte);
                break;
            case "2":
                Intent intentAusbildungsmethoden = new Intent(getApplicationContext(), Ausbildungsmethoden.class);
                startActivity(intentAusbildungsmethoden);
                break;
            case "3":
                Intent intentFallbearbeitung = new Intent(getApplicationContext(), Fallbearbeitung.class);
                startActivity(intentFallbearbeitung);
                break;
            case "4":
                Intent intentBonus = new Intent(getApplicationContext(), Bonus.class);
                startActivity(intentBonus);
                break;
            case "5":
                Intent intentGeneralprobe = new Intent(getApplicationContext(), Generalprobe.class);
                startActivity(intentGeneralprobe);
                break;
        }

Upvotes: 2

Related Questions