Search code examples
androidandroid-intentandroid-activity

how to decide which activity we came from?


Hello Old sports !

To the point, I have 3 activity as follows :

-Activity A

-Activity B

-Activity C

in activity A I create an intent to go to activity C:

Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);

in activity B I create intent to go to activity C too:

Intent intent=new Intent(getActivity(),C.class);
startActivity(intent);

in activity C I am going to do something different if it's from A and B.

The question is what is the best practice on 'how to let activity C know whether activity A or B that is calling ?

-sorry lack English, greeting from bali..


Solution

  • What you can do here is, you can pass one value say flag = "A" when it is from Activity A and flag = B when it is from Activity B via Intent and get that value in Activity C ...

    In Activity A

    Intent intent = new Intent(this, C.class);
    intent.putExtra("flag", "A");
    startActivity(intent);
    

    In Activity B

    Intent intent = new Intent(this, C.class);
    intent.putExtra("flag", "B");
    startActivity(intent);
    

    In Activity C

        Intent intent = getIntent();
        String checkFlag= intent.getStringExtra("flag");
    
    if(checkFlag.equals("A");
    // It is from A
    
    else
    // It is from B