Reputation: 23
I have three classes one main-activity(named MainMap), one non-activity class(named MyItemizedOverlay), and one activity class(named AudioStream). I want to start AudioStream activity from non-activity class but i don't know how to. i tried this is in third class(called MyItemizedOverlay):
Intent myIntentA = new Intent(MainMap.this, AudioStream.class);
myIntentA.putExtra(AUDIO_STREAM,AUDIO_STREAM_URL);
MojProg.this.startActivity(myIntentA);
but it doesn't work, says: No enclosing instance of the type MainMap is accessible in scope
What should i do? What shoul i write instead of MainMap.this?
Upvotes: 2
Views: 5687
Reputation: 280
Intent myIntentA = new Intent(MainMap.this, AudioStream.class); myIntentA.putExtra(AUDIO_STREAM,AUDIO_STREAM_URL); MojProg.this.startActivity(myIntentA);
This won't work. Because "this" means "this class". You cannot use it on another class (Yes, you can, but there are different ways for it. Please study on "this" at forums, this site or oracle web site.). It is the reason of that warning.
Well it looks like your question is "How can i pull the context to a non-activity class?". (First parameter of Intent() is Context).
To do this you can create a Context instant in your Main Activity and assign your base context to it like:
static Context context;
....
context = this.getBaseContext();
Don't forget that was your Main Activity. Then in your non-activity class, you can pull this context and use it with intent like:
Context context;
Intent intent;
....Constructor:
context = MainActivity.context;
intent = new Intent(context, YourSecondActivity.class); // you have to declare your second activity in the AndroidManifest.xml
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // this is required to call "Intent()" in a non-activity class.
//And then you can call the method anywhere you like (in this class of course)
context.startActivity(intent);
Ok. You are ready to go after one more step. In AndroidManifest.xml, declare your second activity like first one like;
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".YourSecondActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
</activity>
You are ready now. But a last warning, dont forget to dispose your activity before opening another to avoid lagging. Have fun.
Upvotes: 0
Reputation: 587
This isn't so much an Android question as it is a Java question. Unless you were to make "MyItemizedOverlay" an inner class of "MainMap" (see http://forums.sun.com/thread.jspa?threadID=690545), what you really need is for MyItemizedOverlay to store an internal reference to the MainMap object that it wants to use for the inent.
Regards, Mark
Upvotes: 2