Start an Activity not from an Intent - android

How can I start an Activity without using an Intent? The only rule I have got is
if( var == true ) startActivity();
but startActivity(); needs an Intent as a parameter.

Just create a new intent for the activity you want to start. depending on where you are you will need the app context thought.
Intent i = new Intent(getApplicationContext(), YourActivity.class);
startActivity(i);

Here's how to navigate to a second Activity (another page) using an Intent.
public void onClick(View v)
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
Also, don't forget to adjust the AndroidManifest.xml for each Activity.
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<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>
</activity>
<activity android:name="SecondActivity"
android:label="#string/second_label">
<intent-filter>
<action android:name="android.intent.action.SECOND" /> //should be namespace of your company I guess
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Related

Why can't I access my activity with the package name?

I have an app A and an app B, I'm trying to open an app B activity from app A, which is not the main activity.
I know there are already lot of answers on this subject, but I can't manage to make any of these work, this is why I'm asking for help.
First here is the "simplified" manifest of the app B (the one that has to be opened) :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rlaville.gestionsav" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".Vues.CreationSav"
android:windowSoftInputMode="stateHidden"
android:noHistory="true">
</activity>
<activity android:name=".Vues.GestionSav" android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Vues.StatSav" android:noHistory="true">
</activity>
<activity android:name=".Vues.ModifierSav" android:noHistory="true" android:windowSoftInputMode="stateHidden" android:exported="true" >
<action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
<provider
android:name=".Donnees.SavDataProvider"
android:authorities="com.example.rlaville.gestionsav.Donnees.SavDataProvider"
android:exported="true"
></provider>
</application>
</manifest>
The activity I want to open is ModifierSav, so I did put an intent filter inside of it as it is said to do in every tutorial.
Then I've tried these methods in the other app :
Directly calling the intent filter name :
Intent i = new Intent("com.example.rlaville.gestionsav.MODIFY_ACTIVITY");
i.putExtra("CurrentSavID", itemId);
startActivity(i);
Or using a packageManager :
public boolean openSavApp(Context ctx, String packageName, long itemId){
PackageManager manager = ctx.getPackageManager();
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new PackageManager.NameNotFoundException();
}
i.putExtra("CurrentSavID", itemId);
ctx.startActivity(i);
return true;
}
that I tried to call like this
openSavApp(getActivity(), "com.example.rlaville.gestionsav.MODIFY_ACTIVITY", itemId);
What am I doing wrong? What should I do to be able to open this specific activity from the other app?
You have missed <intent-filter> tag.
<activity android:name=".Vues.ModifierSav" android:noHistory="true" android:windowSoftInputMode="stateHidden" android:exported="true" >
<action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
change this to
<activity android:name=".Vues.ModifierSav" android:noHistory="true" android:windowSoftInputMode="stateHidden" android:exported="true" >
<intent-filter>
<action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Update:
you are getting null intent because you are not providing the correct package name here.
you have provide here this package name which you mentioned in manifest tag.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rlaville.gestionsav" >
public boolean openSavApp(Context ctx, String packageName, long itemId){
PackageManager manager = ctx.getPackageManager();
Intent i = manager.getLaunchIntentForPackage(packageName);
if (i == null) {
return false;
//throw new PackageManager.NameNotFoundException();
}
i.putExtra("CurrentSavID", itemId);
ctx.startActivity(i);
return true;
}
<intent-filter>
<action android:name="com.example.rlaville.gestionsav.MODIFY_ACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
You've forgot to put action and category inside intent-filter block.

Robolectric getNextStartedActivity always returns launcher activity

I'm trying to test that calling a simple method in my Android app will start another activity. getNextStartedActivity always returns the launcher activity.
Here is the test:
#Test
public void clickingButton_shouldStartNextActivity() throws Exception {
CurrentActivity activity = Robolectric.setupActivity(CurrentActivity.class);
activity.startNextActivity();
Intent expectedIntent = new Intent(activity, NextActivity.class);
assertEquals(expectedIntent, Shadows.shadowOf(activity).getNextStartedActivity());
}
And here is the startNextActivity method in CurrentActivity:
public void startNextActivity() {
startActivity(new Intent(this, NextActivity.class));
}
When I run the test, I get LoginActivity (launcher activity) as the actual result. Here LoginActivity in AndroidManifest.xml:
<activity
android:name=".LoginActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Why other activity is destroying when 'Home' key is pressed.?

There are two activities ActivityA and ActivityB, both are made singleTask. Here ActivityB is of category HOME and it is set to always. I am starting ActivityA from a BroadcastReceiver on ACTION_BOOT_COMPLETED, it is starting ActivityA as expected but when HOME KEY is pressed (which is ActivityB), ActivityA is getting destroyed.
What can be the possible reason of it? How can I stop ActivityA from being destroyed? By keeping both activities singleTask.
This is the BroadcastReciever:
public class MyStartupIntentReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BOOT_COMPLETED == intent.getAction()) {
Intent i = new Intent(context, ActivityA.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);
}
}
}
And manifest file is as follows:
<activity
android:name="ActivityB"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name="ActivityA"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>

Calling another activity in android

I am working in android development, I am trying to call another activity (activity 2) when a button is clicked
here is the code of button which is to be clicked to call the second activity
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/editText1"
android:layout_marginLeft="41dp"
android:layout_toRightOf="#+id/editText1"
android:text="Button1"
android:onClick="button1_func" />
the code of activity calss for this button
public void button1_func()
{
Intent i=new Intent ("second_activity");
startActivity(i);
}
ad here is manifest.xml
<activity
android:name="com.example.application1.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.application1.Secondactivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.second_activity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
but my app crashes when I click the button whats the problem??
Here it is
Intent intent = new Intent(Activity1.this,Activity2.class);
startActivity(intent);
You have
android:onClick="button1_func" />
So you need
public void button1_func(View v) // change method signature
{
// do something
Intent i=new Intent (this,Secondactivity.class);
startActivity(i);
}
and remove the below for SecondActivity in manifest
<intent-filter>
<action android:name="android.intent.action.second_activity" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
Read about Explicit and Implicit intents #
http://developer.android.com/guide/components/intents-filters.html
You have to use
Intent i=new Intent (MainActivity.this,SecondActivity.class);
in place of
Intent i=new Intent ("second_activity");

android intent filter?

I'm feeling stupid.This is very clear but I can not solve my problem.So excuse me for my question.
My problem is in about intenfilter.This is application tag of my manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AlakyTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="reza"
android:name=".A2" >
<intent-filter >
<action android:name="MAIN" />
<category android:name="LAUNCHER" />
</intent-filter>
</activity>
</application>
And this is my button click listener:
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent2 = new Intent();
intent2.setAction("MAIN");
intent2.addCategory("LAUNCHER");
startActivity(intent2);
}
});
I think that all things is good but when I run my code and click on b1,I get this erroe:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=MAIN cat=[LAUNCHER] }
Edit:
This is A2:
public class A2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main2);
}
}
Please help me.
You should set the android:name of the second activity to the (package name).(the class)
for example, lets say the second activity class is 'com.my.app.reza' you should add you the manifest:
<activity
android:label="#string/app_name"
android:name=".reza" >
<intent-filter >
<action android:name="com.my.app.REZA" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
and you should start the activity like that:
Intent intent = new Intent("com.my.app.REZA");
startActivity(intent);
NOTE that it isn't the best way to do it, you shouldn't mess to much with package name I'd recommend you to do it the following way:
<activity
android:label="#string/app_name"
android:name=".reza" />
and start it like that:
startActivity(new Intent( this.getContext() , reza.class );
Please use like that:
Intent intent2 = new Intent(context,A2.class);
startActivity(intent2);
android:name=".A2" ,you must have "A2" activity class implement!
Modify to android:name=".A2", not android:name="A2"!
You don't need to specify category if you just need to call A2 inside your app. And you should set an unique action name, for example it can be a hash string:
...
Intent intent2 = new Intent("a202bfa923069ee8e119205e3468ee131ceafda37");
startActivity(intent2);
Note that action name uses same rule as package name.
<intent-filter>
<action android:name="com.blacky.basictutorial.TutorialTwo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Try to use this in your second activity and call by the following code:
startActivity(new Intent("com.blacky.basictutorial.TutorialTwo"));
Hope this will work for you.

Categories

Resources