0

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

Agent Smith
  • 23
  • 1
  • 7

5 Answers5

1

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.

Stephan Branczyk
  • 9,144
  • 1
  • 29
  • 49
  • Thanks for the tip sir. I'll try to search for it. You are right, I can't combine the two projects into one due to some conflicts and I think to make things a lot easier, I separated the two projects. – Agent Smith Aug 19 '14 at 05:53
0

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

0

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

}

  • I have a question. Is there some way that I can make the dialogIntent variable accessed in a button from another java file? The button that I need to place the Intent is in another java file in the MainActivity1 Project. – Agent Smith Aug 19 '14 at 06:25
  • it's not a problem, you can put it there as long as your class is extend the activity or what else that can support "startActivity" – user3905216 Aug 19 '14 at 06:34
0

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.

Community
  • 1
  • 1
S.Basnagoda
  • 541
  • 1
  • 7
  • 19
0

Use this example

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

you need to start activity in manifest file also in tag

<application>
       <activity android:name=".MainActivity2"></activity>
</application>
Suresh
  • 258
  • 1
  • 5
  • 17