Agent Smith
Agent Smith

Reputation: 21

Android Java Eclipse Intent with 2 Projects

Good day!

I have 2 Projects in my Eclipse. MainActivity1 and MainActivity2 are the names of the activities. The MainActivity2 is connected to the MainActivity1 via Project in the properties of the MainActivity1. So far, I can call the package of the MainActivity2 from the MainActivity1. Now, below are my problem:

I have a button in MainActivity1. What I want to do is after I click on the button in the MainActivity1, it runs the MainActivity2 project and some variables will be passed on to the MainActivity2.

I've been searching in google for a while now and I couldn't get the right keyword for it. I've checked some of the questions here in StackOverflow but I'm unable to locate the problem similar to mine with calling and running the MainActivity2 class from the MainActivity1.java

I'm a noob in Android Java but have knowledge in other OOP.

Any tips will be a great help. Thanks

Upvotes: 0

Views: 171

Answers (5)

Suresh
Suresh

Reputation: 427

Use this example

Intent intent = new Intent();
intent.setComponent(new ComponentName("your_package_name", "your_package_name.MainActivity2"));
startActivity(intent);

you need to start the activity in the manifest file and also in <application> tag

<application>
       <activity android:name=".MainActivity2"></activity>
</application>

Upvotes: 0

user3905216
user3905216

Reputation: 1

Put this to your MainActivity1 in your button

Intent dialogIntent = new Intent(getBaseContext(), MainActivity2.class);
dialogIntent.putExtra("message", "calling MainActivity2.class");
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplication().startActivity(dialogIntent);

and put this to your MainActivity2 inside onCreate()

String message = getIntent().getStringExtra("message");
if(message != null){
    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
}

Upvotes: 0

S.Basnagoda
S.Basnagoda

Reputation: 721

I think this is what you are looking for. Use intent.SetCompenent() method to tell MainActivity1 about MainActivity2.

Intent intent = new Intent();
intent.setComponent(new ComponentName("your_package_name", "your_package_name.MainActivity2"));
startActivity(intent);

Another few possible answers found here and here. I'm unable to check them right now, so you can try them by yourself. Hope that those are useful.

Upvotes: 0

BatmanGotham
BatmanGotham

Reputation: 1

You should change 1 of your project into library project. You can change it by right click your project properties > Android > check is Library checkbox

And then include it into another project by right click your project > Android > add ... choose the library, and apply it.

If you want to include the activity, you should put it in manifest file

activity android:name="com.your.package.MainActivity2"

that's my opinion

Upvotes: 0

Stephan Branczyk
Stephan Branczyk

Reputation: 9375

Most likely, you should just combine both projects into one.

If for some reason, you really can't do that. Search for the term explicit intent. And search for the term onClickListener for your button to know that it is being clicked and that it needs to open an activity when that happens.

And please, don't use the default package names for your projects, create your own unique package names following the normal Java conventions.

Upvotes: 1

Related Questions