0

In going from an activity to another one by an intent, How can I get class loader name ? I need name of class loader. If my intention wasn't clear, Please look at this codes.

I created a test app for this Question. It has three activity. ActivityA , ActivityB and ActivityC. I have a button in ActivityA and ActivityB that start activityC.

ActivityA:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_a);

        Button btnGoFromActivityAToActivityC = (Button) findViewById(R.id.btnGoFromActivityAToActivityC);

        btnGoFromActivityAToActivityC.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(ActivityA.this, ActivityC.class);
                startActivity(intent);

            }
        });
    }

ActivityB:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_b);

        Button btnGoFromActivityBToActivityC = (Button) findViewById(R.id.btnGoFromActivityBToActivityC);

        btnGoFromActivityBToActivityC.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(ActivityB.this, ActivityC.class);
                startActivity(intent);

            }
        });
    }

ActivityC:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_c);

        TextView textViewNameOfFirstActivity = (TextView) findViewById(R.id.textViewNameOfFirstActivity);

   if (/*  if we came from ActivityA  */)
   {
       textViewNameOfFirstActivity.setText("You came from ActivityA");
   }

   else if (/*  if we came from ActivityB  */)
   {
       textViewNameOfFirstActivity.setText("You came from ActivityB");
   }
    }

Look at ActivityC...

How can I do this ?

MostafaUTD
  • 29
  • 6

1 Answers1

2

The ClassLoader isn't going to tell you what loaded your Activity; it's for loading Classes.

A better approach would be to send some kind of info to Activity C that tells it who opened it. e.g.

Activity A:

Intent intent = new Intent(ActivityA.this, ActivityC.class);
intent.putExtra("from", "Activity A");
startActivity(intent);

And then in your onCreate(Bundle) method of Activity C you would get that value:

@Override
public void onCreate(Bundle savedInstance){
    ....

    String fromActivity = getIntent().getStringExtra("from");

    textViewNameOfFirstActivity.setText(fromActivity);
}
Cruceo
  • 6,763
  • 2
  • 31
  • 52
  • Well actually that's a good idea .. but as you know, intent will be executed inside a class. I mean when you click on a button, it opens a class in first, and then intent executes. So intent is defined into that class. **My question:** How can I check whether if the name of class *(which is called)* equals to classA, then do sth? – MostafaUTD Aug 18 '16 at 23:10
  • 1
    @MostafaUTD This is the correct approach. You are either using the wrong words or you don't understand how this works. Clicking on a button doesn't "open a class", it **calls a method on an instance of a class**. An `Intent` does not "execute". An `Intent` is just a data container, like a "message". When you call `startActivity()`, you give the Android framework an `Intent` and say "find an `Activity` that can handle this `Intent` and start it for me please". Android does not provide any built-in way to determine what component called `startActivity()`. – David Wasser Aug 19 '16 at 10:33