ming_codes
ming_codes

Reputation: 2922

Can multiple Activity declarations share the same class name?

For example

<manifest>
    <activity android:label="MyActivity1" android:name=".MyClass">
    </activity>
    <activity android:label="MyActivity2" android:name=".MyClass">
    </activity>
</manifest>

Upvotes: 1

Views: 1394

Answers (2)

Phil
Phil

Reputation: 36289

Yes and no. When you prepend the Activity name with a ., it looks at the Manifest's default package to get the whole class path, such as com.example.android.MyClass. Thus, if instead you have one .MyClass and another com.example.android.other.MyClass, then this should work.

<manifest>
  <package="com.example.android">
    <activity android:label="MyActivity1" android:name=".MyClass">
    </activity>
    <activity android:label="MyActivity2" android:name="com.example.android.other.MyClass">
    </activity>
</manifest>

Upvotes: 1

sgarman
sgarman

Reputation: 6182

I'm not 100% sure if this is possible, but perhaps there is a better way to go about this. If you need the same activity you can call it in both situations like you normally would but pass in data during the call as well. In your MyClass you can read the data and decide how to handle it.

Example: //Activity 1

    Intent i = new Intent(this, MyActivity.class);
    i.putExtra("open", "activity1data");
    startActivity(i);

//Activity 2

    Intent i = new Intent(this, MyActivity.class);
    i.putExtra("open", "activity2data");
    startActivity(i);

And in MyActivity do something like this in onCreate()

   Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    String action = intent.getAction();
    if(extras.containsKey("open")){
         if(extras.getString("open").equals("activity1data")){
              //DO activity 1 stuff
         }
    }

This is a pretty rough example, you could use ints and switch on those etc. But the real goal is to let one activity handle a variety of cases. This seems to be what you want since your going to use the same class anyway.

Upvotes: 1

Related Questions