Get intent Class loader . I want to set text a tex

2019-03-04 03:46发布

问题:

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 ?

回答1:

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);
}