Is it possible to call the mainActivity class of another project ? - java

I have 3 android projects, which they have different functions. Each project has its own mainActivity class.
Now I want to merge the 3 projects into one. I can think of 2 ways to do it.
First way: open one of the 3 projects, then create new java class and xml files and copy all java class and xml content from other 2 projects. However, each project has its own mainActivity class. I don't know whether I should preserve one mainActivity class only and rename other 2 mainActivity class of other 2 projects. It seems that it is easy to encounter lots of bugs and messy.
Second way: use 3 buttons. onclick first button then the mainActivity class of project 1 will run. onclick second button then the mainActivity class of project 2 will run. onclick third button then the mainActivity class of project 3 will run. However, I don't know whether it is possible to do so. If yes, How can I do so ? I know how to onclick on a button to go to a new activity but is that the same to apply to onclick going to other project's mainActivity ?
I prefer the second method, if it is possible to do so. Could anyone help ? Please advise , thanks.

This think it's impossible, you need to import all functions in 1 project...
You can't open other project with one button, you need to import classes and activities in your global project that you needs or create a new classes, but you can't call other projects. Good luck!

If you add a custom action to your activity:
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="com.example.action.MYACTION" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
You can invoke it via implicit intent as long as the app already installed on the device:
Intent i = new Intent("com.example.action.MYACTION");
startActivity(i);

Related

How do you change the default dialer package in android studio?

I'm currently trying to change the default dialer package of the machine to my application. When running the code that is supposed to change it, however, it results in the default dialer package retaining a value of NULL. This is loosely following the guide here, but some of it is deprecated so I've had to make some adjustments.
EDIT: has REQUEST_CODE_DEFAULT_DIALER been changed somehow? I can't find it referenced anywhere other than outdated tutorials on how to do this, so I'm not sure what it would even have been changed to. I'm getting a "cannot resolve symbol" error whenever I try to use it.
I've included the following under the relevant activity in my AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.DIAL" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme = "tel"/>
</intent-filter>
The following code executes the package change and then prints its value to logcat so I can see what happens:
Log.i("Before", "Before default dialer change");
startActivity(new Intent(TelecomManager.ACTION_CHANGE_DEFAULT_DIALER).putExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME,getPackageName()));
TelecomManager telecomManager = ((TelecomManager) getSystemService(Context.TELECOM_SERVICE));
if(telecomManager.getDefaultDialerPackage() == null) {
Log.i("Default dialer package:", "NULL");
} else {
Log.i("Default Dialer Package:", telecomManager.getDefaultDialerPackage());
}
I'm getting "Default dialer package:: NULL" in logcat every time I try this.
Intents are asynchronous. If you start an activity to do something, you have to wait for that intent to finish before using the results of it. You're instead immediately querying for the default dialer package. That will always fail, because it wouldn't even have shown the picker on screen yet.

onCreate runs 2 times with singletop launch mode set

Problem:
My Launching Activity creatively named LoadingActivity's onCreate method is called 2 times.
Manifest Content:
<manifest>
<application>
...
<activity
android:launchMode="singleTop"
android:name="com.awesome.app.activites.LoadingActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
...
</application>
</manifest>
First onCreate call is called with Bundle savedInstanceState which is null, shortly after which the onCreate method is called again, but this time with Bundle savedInstanceState which contains the following serialized content:
Bundle[{com.google.app_measurement.screen_service=Bundle[{referrer_name=LoadingActivity, id=1612091517631091562, name=null}], android:viewHierarchyState=Bundle[{android:views={16908290=android.view.AbsSavedState$1#a24446f, 2131296302=android.view.AbsSavedState$1#a24446f, 2131296313=android.view.AbsSavedState$1#a24446f, 2131296424=android.view.AbsSavedState$1#a24446f, 2131296445=android.widget.ProgressBar$SavedState#b25257c, 2131296581=android.view.AbsSavedState$1#a24446f, 2131296582=android.view.AbsSavedState$1#a24446f}}], androidx.lifecycle.BundlableSavedStateRegistry.key=Bundle[{}], android:lastAutofillId=1073741823, android:fragments=android.app.FragmentManagerState#a24ee05}]
Yes, this doesn't mean much, but it may be related to something at the bottom of the post.
Tried:
I have tried setting launchmode settings with android:launchMode="singleTop" for the LoadingActivty in my AndroidManifest.xml, but this does not help at all, it still runs the onCreate 2 times.
I have gone through my onCreate method, no where do I onCreate again or change ApplicationContext settings i.e. Orientation (physically or programmatically), etc.
Any idea why this happens and what I can do to resolve it.
For what it is worth, I have recently updated my deps and see this in my Manifest:
It compiles (change the package name for obv reasons) with the portrait launchmode, but I think this may have something to do with the double launch

Start activity from another package - Issue

I downloaded library project from github and imported it into my app project.The problem is that i want to start activity from this library in my main app class.I tried many ways and one of them was:
Intent intent = new Intent();
intent.setComponent(new ComponentName(
"com.tehedligmail.restorancafeler.RestaurantActivity
,
"com.cunoraz.pickImages.MainActivity"));"
startActivity(intent);
This is my log:
08-29 04:40:13.937: E/AndroidRuntime(11778):
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.tehedligmail.restorancafeler.RestaurantActivity/com.cunoraz.pickImages.MainActivity};
have you declared this activity in your AndroidManifest.xml?
_---------------------------------------------------------------------------
i tried this but manifest can't show the package and activity class:
<activity android:name="com.luminous.pick.MainActivity">
<intent-filter>
<action android:name="com.luminous.pick.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
As i understand from log i should define the class that i want to launch in my manifest,but i couldn't write this,even autocomplete of manifest didn't show this class's name.
Thanks in advance.
For more information This is image shows library and main project of my app
As the document, I think the package name is problem. Can you try this?
intent.setComponent(new ComponentName( "com.cunoraz.pickImages", "com.cunoraz.pickImages.MainActivity"));"
And no need to declare this activity in AndroidManifest.xml

Android Manifest- intent filter and activity

Could someone explain the following lines in the manifest -
<activity
android:name=".AboutUs"
android:label="#string/app_name">
<intent-filter >
<action android:name="com.example.app1.ABOUT" />
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
How are the fields in activity and intent filter important and when are they used/referred ?
Sorry, i tried to read the documentation but still couldnt figure much out.
Thank you
android:name=".AboutUs"
This is the name of your Activity class, the dot at the front is shorthand notation for your package. So this actually stands for com.your.package.name.AboutUs which means your java file that represents this Activity is called AboutUs.java
android:label="#string/app_name"
label is the string that gets shown in the launcher(if the activity is listed in the launcher) and at the top of the window when the activity is open.
<intent-filter > ... </intent-filter>
intent filter defines the Intents that your activity "listens for" in order to launch.
<action android:name="com.example.app1.ABOUT" />
<category android:name="android.intent.category.DEFAULT"/>
Action and category are both fields that get set on an Intent before it is "fired off" into the system. The system will then look for any activities that match both the action and category and if it finds one then it will launch that activity, or if it finds multiple it will show the user all of them and let them pick.
In your case your the action you are listening for com.example.app1.ABOUT is a custom action that is specific to your app, not one of the systems actions.
So here is what an intent that would start this particular activity might look like:
Intent i = new Intent();
i.setAction("com.example.app1.ABOUT");
i.addCategory("android.intent.category.DEFAULT");
startActivity(i);
Note that because you've created a custom action, this intent does not require access to your AboutUs.class so this intent could technically be fired from any app on the device and it would launch into your activity.
For the ACTION you have this answer and for the DEFAULT category you have this answer
An Activity is an application component that provides a screen with which users can interact in order to do something, such as dial the phone, take a photo, send an email, or view a map. To declare your activity, open your manifest file and add an element as a child of the element like your example.
The android:name attribute is the only required attribute—it specifies the class name of the activity.
The android:label attribute is a user-readable label for the application as a whole, and a default label for each of the application's components
An element can also specify various intent filters—using the element—in order to declare how other application components may activate it.
The element specifies that this is the "main" entry point to the application.
The element specifies that this activity should be listed in the system's application launcher (to allow users to launch this activity).
Please refer to the http://developer.android.com/guide/components/activities.html

Confusion over execution order

I have a game application made up of several different activities. The first to be called is a splash screen, when this completes, this finishes and starts up another activity via an intent. In order to have access to some global data that is consistent across all the activities, I also have a "globals" class like this:
public class Globals extends Application
{
int global_variable_A;
int global_variable_B;
int global_variable_C;
public void onCreate()
{
// stuff
}
}
In the androidmanifest.xml I have the the following (amongst other things):
<application
android:icon="#drawable/mygame_icon"
android:screenOrientation="portrait"
android:label='"My Game"' android:name=".Globals">
<activity
android:label="My Game"
android:name=".Splash"
android:screenOrientation="portrait">
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My question now is, which will be executed first, the onCreate of Globals or the onCreate of Splash? Or are they run at the same time on different threads? I ask because I'm getting some inconsistent behaviour that would be explained if they were on different threads.
onCreate() Global off course.. Application gets executed first and then the Activity,,.. you can test for yourself by keeping a debug point in Application onCreate() method..

Categories

Resources