Alex
Alex

Reputation: 1232

Can't start activity from library

I'd like to start activity from a library. My little test goes as follows:

I put this code into main activity this is not a library:

public class MainActivity extends Activity {

    MyTest myTest;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myTest = new MyTest(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void helpPressed(View view) {
        myTest.startNewActivity();
    }
    public void localHelpPressed(View view) {
        Intent helpIntent = new Intent(this, LocalHelp.class);
        startActivity(helpIntent);
    }
}

And this code into 'myTest' which is in a library:

public class MyTest {
    Context context;
    public MyTest(Context context) {
        this.context = context;
    }
    public void startNewActivity() {
        Intent helpIntent = new Intent(context, HelpActivity.class);
        context.startActivity(helpIntent);
    }
}

I added 'HelpActivity' to library, and LocalHelp to application.

The code works fine creating 'LocalHelp' (by calling localHelpPressed method), but not 'HelpActivity' (by calling helpPressed method). 'HelpActivity' gives this error:

01-16 23:49:17.940: E/AndroidRuntime(4736): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {test.activity/com.example.test.activity.library.HelpActivity}; have you declared this activity in your AndroidManifest.xml?
01-16 23:49:17.940: E/AndroidRuntime(4736):     at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1556)
01-16 23:49:17.940: E/AndroidRuntime(4736):     at android.app.Instrumentation.execStartActivity(Instrumentation.java:1431)
01-16 23:49:17.940: E/AndroidRuntime(4736):     at android.app.Activity.startActivityForResult(Activity.java:3391)

Is what I'm trying even possible? - for a library to create activity which lives inside library?

For now (in my actual application) I moved my help screen to project, and created a callback, which library calls when it needs a help screen. This works, but doesn't sound right, plus every project which includes library would need to have this callback method.

Upvotes: 2

Views: 2234

Answers (1)

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21201

In your manifest file declare the help activity

<application ... >
    <activity
        android:name="com.example.test.activity" >...

Upvotes: 3

Related Questions