Android library project custom redirect - android

I’ve an android app and checked as library let it be Mainapp. Now I have created two separate app using that library viz. subapp1 and subapp2. Individual apps are running fine. I’ve login activity in the library package. On successful login user will be redirected to dashboard activity.
Written simply in loginactivity page of the library pack
Intent i = new Intent();
i.setClass(getApplicationContext(), UserhomeActivity.class);
startActivity(i);
Now I need to specify to which activity the user will be redirected based on the sub app. So how I can manage this without replicating the pages. Thanks.

you could provide the class and package name as an argument and start a new intent:
Intent sccuess = new Intent();
sccuess.setClassName(packageName, className);
startActivity(sccuess);

I am not sure I understand you correctly but If I understand you correct, you need to derive your activities from SecureActivity which user is supposed to be logged in to access there.
After you extend your classes from SecureActivity, you can check it in onResume() method.
Here is an example:
public class SecureActivity extends Activity
{
#Override
public void onResume()
{
// Check if user logged in or not.
}
}
public class YourActivity extends SecureActivity
{
// ...
}

In the Mainifest of your Library, your should define like that
<activity
android:name="com.gmail.app.activities.A_Activity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="com.gmail.app.A.Fire_Activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.gmail.app.activities.B_Activity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/NoActionBar" >
<intent-filter>
<action android:name="com.gmail.app.B.Fire_Activity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
In library code where to fire the intent:
Intent i = new Intent(_context.getPackageName() + ".Fire_Activity");
//Action will be like com.gmail.app.A.Fire_Activity or com.gmail.app.B.Fire_Activity
startActivity(i);
PS:
Your Sub-Apps have package-names:
com.gmail.app.A
com.gmail.app.B

Related

How do I make an activity the first activity launched but only on the first run of the application

I need my app to have one activity(Register) be the first activity launched when an app is first installed but then after the initial run it will have a different activity(LogInActivity) be the first activity launched... I have put code in that makes the Register activity only run once but I can't figure out how to alter my manifest to get the desired results. I have tried moving the MAIN action to the Register activity but that causes it to be launched everytime I run my app. When the MAIN action is in the LogInActivity then Register doesn't run at all. I also tried to add a DEFAULT action in my manifest under the opposite activity of where I had the MAIN action but that didn't work either and upon further research of what DEFAULT does I don't believe this is what I need. So is there some other intent I need to add to my XML?
Manifest:
<application
android:allowBackup="true"
android:icon="#drawable/skey"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
///////////FIND INFO ACTIVITY
<activity
android:name=".FindInfoActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LogInActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//////////LOG IN ACTIVITY
<activity
android:name=".LogInActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LogInActivity" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
//////////REGISTER ACTIVITY
<activity
android:name=".Register"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.LogInActivity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Don't have three separate LAUNCHER activities unless you want the user to be able to launch the app from the homescreen from three separate items (there are valid use-cases for this, but this isn't it).
Have one activity that handles launching the application. It checks the boolean in your SharePreferences, then starts the appropriate activity from there. Then just finish the Activity so the user won't re-open it when the user presses "Back".
public class LauncherActivity extends Activity {
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
if (firstTimeRunning()) {
// start register activity.
} else {
// start login activity.
}
finish();
}
}
In the Manifest just have this Activity as:
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
You don't need any IntentFilters for your register activity or login activity because these are never opened through implicit intents, nor do they have any special permissions (from what I can see).
Use SharedPreference. When you open the app the first time do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
shared.edit().putBoolean("first_time",true).apply();
in your onCreate() of first Activity do this:
SharedPreferences shared;
shared=getSharedPreferences("com.example", Context.MODE_PRIVATE);
if(shared.getBoolean("first_time",false)){
startActivity(new Intent(FirstActivity.this, NewFirstActivity.class));
finish();
}
Is the easiest way to "storage" data and states. SharedPreference also works with Strings, Int values, etc (if you don't want boolean value)
In your Manifest you should have only one launcher class, in the same class's onCreate method check for already logged in condition (SharedPreference). If yes, then using Intent launch HomeActivity otherwise launch LoginActivity if the user has registered and logged out. When user logs in, save account id / session details / user details in SharedPreference which you can validate in your launcher class for starting specific activity.
As Mariano said, you can use SharedPreferences. If you don't want to open your settings at startup you can use a Dialog to let the user decide to go to your settings or do nothing.
By the way, I think that use SharedPreferences to store login information (username and password) is best practice than be asking these values every time you start an app.

Android Lollipop App Notification Settings

In Android Lollipop, when you long press a Notification, it gives you access to settings for that Notification's app, such as priority, or simply blocking it. Is there an intent that I can use to access those settings?
Found the answer here - https://plus.google.com/+CyrilMottier/posts/YY6tbJrJMra
You need to add this to the activity you want to be attached to that settings icon
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.NOTIFICATION_PREFERENCES" />
</intent-filter>
In addition to the correct answer from #shmuel :
In case you want this "App settings" system button to jump to one specific fragment of your app's Settings activity deriving from PreferenceActivity, you can create a dummy intermediary activity to handle the intent-filter as details above, and then in this activity you simply do:
public class DeepSettingsFragmentDummyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent settingsIntent = new Intent(this, SettingsActivity.class);
// Standard mechanism for a PreferenceActivity intent to indicate
// which fragment to activate automatically.
settingsIntent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT,
YourTargetPreferenceFragment.class.getName());
startActivity(settingsIntent);
finish();
}
}
This code automatically launches the YourTargetPreferenceFragment fragment, and then dismisses itself (finish()) so as to not remain in the activity stack when the user hits Back.
Your Manifest must contain:
<activity
android:name=".DeepSettingsFragmentDummyActivity"
android:label="...">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.NOTIFICATION_PREFERENCES" />
</intent-filter>
</activity>

Dropbox Core API for Android: Not returning after Authentication

I am developing two android applications:
the first is a normal application with a launcher and so on
the other is a application only with a viewer activity (see the manifest):
<activity
android:name=".MyActivity"
android:icon="#drawable/icon"
android:label="#string/dropbox" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="com.dropbox.client2.android.AuthActivity"
android:configChanges="orientation|keyboard"
android:launchMode="singleTask" >
<intent-filter>
<data android:scheme="db-XXXXX" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The plan is, that the first application does not need internet permissions and the second is some kind of add-on to the first.
The second application should sync a file with Dropbox (with the Core API, not with the Sync API).
From my first app, I start 'MyActivity' from the second app like this:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName("my.package","my.package.MyActivity"));
intent.putExtra("filePath", "/myFile.txt");
startActivityForResult(intent, SYNC_REQUEST);
That works. The Activity comes up and there, if not authorized yet, the user must press a button. Then the following code will be executed
AndroidAuthSession session = buildSession();
mApi = new DropboxAPI<AndroidAuthSession>(session);
mApi.getSession().startAuthentication(MyActivity.this);
If the user does not have dropbox installed, the browser will pop up.
Now my troubles begin:
As soon as the user presses 'Accept' or 'Decline', the browser does not disappear. It stays open and MyActivity does not get resumed (onResume is not called!).
I found out, that adding
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
before starting MyActivity from my first application will solve the problem, but then I can not listen/wait for the result of MyActivity.
I am frustrated. Can anyone help me or give me some advise?
Thanks!
When you are using startAuthentication method. It will automatically start the AuthActivity. you do not need to call startActivity() explicitly. Then in onResume method of your activity write this code :
#Override
protected void onResume()
{
super.onResume();
if (if dropBoxAPI!= null && dropboxAPI.getSession().authenticationSuccessful())
{
try {
dropboxAPI.getSession().finishAuthentication();
oAuth2Token = dropboxAPI.getSession().getOAuth2AccessToken();
}
catch (IllegalStateException ie)
{
ie.printStackTrace();
}
}
}

how to put a shortcut of an activity into the launcher?

i'm trying to do something that is described briefly on the next link:
link
meaning, i have an activity that can be even on another application (but for now let's focus on an activity that my app has) , which i want to be able to create a shortcut to it.
for terminology , let's say that the activity that creates the shortcut is named "ShortcutCreatorActivity" ,and the activity that gets started is "MyActivity" .
what i got from what is written is that the ShortcutCreatorActivity should be defined in the manifest as:
<activity android:icon="#drawable/ic_launcher"
android:label="ShortcutActivity" android:name="com.my_app.ShortcutCreatorActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
and what i got in its java code is:
public class ShortcutCreatorActivity extends Activity
{
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final Intent shortcutIntent=new Intent("com.my_app.MyActivity");
final ShortcutIconResource iconResource=Intent.ShortcutIconResource.fromContext(this,R.drawable.ic_launcher);
final Intent intent=new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT,shortcutIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME,"Shortcut Test");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,iconResource);
setResult(RESULT_OK,intent);
Toast.makeText(this,"shortcut created",Toast.LENGTH_SHORT).show();
finish();
}
}
yet i keep getting the same message of "application not found" when clicking on the shortcut , and the log :
ActivityManager(232): Starting: Intent { act=com.my_app.MyActivity flg=0x10200000 bnds=[80,150][160,250] } from pid 3956
can anyone please help me? what is missing?
i've also tried some intent filters for the MyActivity activity inside the manifest. nothing helped...
#liro
i don't think it matters , since i've specified the exact full path to the class , including the package name.
everyone, please, if you have a working project, that would be perfect .
Not sure here, but I think you need to remove the "com" from "com.MyActivity."
<activity android:icon="#drawable/ic_launcher"
android:label="ShortcutActivity" android:name=".ShortcutCreatorActivity">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Instead of
final Intent shortcutIntent=new Intent("com.my_app.MyActivity");
try
final Intent shortcutIntent=new Intent(this, com.my_app.MyActivity.class);
or define your action in intent-filter of that activity, which I believe you may did not.
Those two links may be helpful:
http://developer.android.com/reference/android/content/Intent.html#Intent(java.lang.String)
http://developer.android.com/guide/topics/manifest/action-element.html

launch activities from different package

I have activity A in package one, and I want to run an intent which will up an activity B which is in package two.
How can I do this? Any samples will be welcome.
this is what ive done, and the error i get:
first activity ("MainActivity") in a package: com.abelski.currencyclient
and second activity("SecondActivity" in a diffrent package: com.idan.second
now i wanna call from MainActivity to SecondActivity.
ive added this line at the manifest of the MainActivity:
<activity android:name="com.idan.second.SecondApplicationActivity"></activity>
now in main Activity i got this button which run this line:
Intent intent = new Intent(MainActivity.this,SecondApplicationActivity.class);
and this is the rror:
04-29 09:20:59.197: ERROR/AndroidRuntime(399): Uncaught handler: thread main exiting due to uncaught exception
04-29 09:20:59.276: ERROR/AndroidRuntime(399): java.lang.NoClassDefFoundError: com.idan.second.SecondApplicationActivity
04-29 09:20:59.276: ERROR/AndroidRuntime(399):
I am assuming that by "packages" you mean applications.
We have:
- ApplicationA with FirstActivity
- ApplicationB with SecondActivity
If, in the ApplicationB's AndroidManifest.xml file, in the declaration of SecondActivity you add an intent filter such as:
<activity android:name=".SecondActivity">
<intent-filter>
<action android:name="applicationB.intent.action.Launch" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
You can create an Intent to launch this SecondActivity from the FirstActivity with:
Intent intent = new Intent("applicationB.intent.action.Launch");
startActivity(intent);
What this all means is:
The SecondActivity has a filter for the intent action of "applicationB.intent.action.Launch"
When you create an intent with that action and call 'startActivity' the system will find the activity (if any) that responds to it
The documentation for this is at: https://developer.android.com/reference/android/content/Intent.html
I had the same problem and the solution was another level in the root of your package name.
If you have two packages "com.first...." and "com.second...", and the manifest is referencing "com.first". Then you can refactor both packages in order to reuse the first part. For instance, "com.package.first" and "com.package.second". Then your manifest has to be also updated
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
...
<activity android:name=".first.FirstPackageActivity" android:label="FirstPackageActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".second.SecondPackageActivity" android:label="SecondPackageActivity">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Your java code can create an intent and start the activity in the usual way:
Intent intent = new Intent(this,ActivityClassName.class);
startActivity(intent);
If the package you mentioned here is same to application,I think the answer in the question Android: Starting An Activity For A Different Third Party App is simpler。With the first answer to that question, you don't need to make any modification to your second application.
Use explicit intents:
Intent intent = new Intent(context,ClassName.class);
where ClassName is from another package.
Sometimes, you will not know the name of the class in such cases you will have to rely on the Intent that the target class advertises to handle.
First, you need to declare both packages and activities on Manifest :
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
and for the second activities, on android:name --> .Packages name.activity, assuming your second packages name com.iden.second :
<activity android:name=".second.SecondActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<intent-filter>
<action android:name="android.intent.action.SECOND" />
<category android:name="android.intent.category.SECOND" />
</intent-filter>
</activity>
then on the MAINACTIVITY JAVA CLASS, asuming you will start second activity using a button :
public class MainActivity extends AppCompatActivity {
private Button mButtonSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonSecond = findViewById(R.id.btn_second);
mButtonSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent newActivity = new Intent(MainActivity.this,com.iden.second.SECOND.class);
startActivity(newActivity);
}
});
}
}
hope that can help, since i am not clear with the question structure

Categories

Resources