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

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.

Related

startActivity() does nothing even when the Intent is specified correctly

I am trying to open an activity from the launcher activity when an FCM-generated notification gets clicked. The startActivity function does nothing even when the log statement written above gets executed. Following are some relevant files/functions, please tell why is the ConversationDetailActivity not starting:
The function which starts another activity from LoginActivity
void handleNotificationClick()
{
if (getIntent().getExtras() != null)
{
for (String key : getIntent().getExtras().keySet())
{
Object value = getIntent().getExtras().get(key);
System.out.println("----- pushNotif Key: " + key + " Value: " + value);
}
Intent intent;
if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_CATEGORY).equals(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
{
intent = new Intent(LoginActivity.this, ConversationDetailActivity.class);
}
else
{
intent = new Intent(LoginActivity.this, ApprovalDetailActivity.class);
if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_APPROVAL_TYPE).equals("I"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
else if(getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_APPROVAL_TYPE).equals("A"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
else
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);
intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
}
intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , getIntent().getStringExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID));
intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
intent.putExtra(Master.KEY_POSITION, 0);
System.out.println("----- starting new activity from handleNotificationClick");
LoginActivity.this.startActivity(intent);
LoginActivity.this.finish();
}
}
The sendNotification function from which the notification is generated
private void sendNotification(String messageBody)
{
Intent intent;
System.out.println("----message body: " + messageBody);
if(notificationBundle.getCategory().equalsIgnoreCase(Master.KEY_PUSH_NOTIFICATION_CONVERSATION))
{
intent = new Intent(this, ConversationDetailActivity.class);
}
else
{
intent = new Intent(this, ApprovalDetailActivity.class);
if(notificationBundle.getApprovalType().equals("I"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_VERIFICATIONS);
else if(notificationBundle.getApprovalType().equals("A"))
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_APPROVALS);
else
intent.putExtra(Master.KEY_WHICH_APPROVAL, Master.KEY_COMPLETED);
intent.putExtra(Master.KEY_IS_FROM_CONVERSATION, false);
}
intent.putExtra(Master.KEY_PUSH_NOTIFICATION_POST_ID , notificationBundle.getPostID());
intent.putExtra(Master.KEY_IS_FROM_PUSH_NOTIFICATION, true);
intent.putExtra(Master.KEY_POSITION, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT | PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.mnet_icon)
.setContentTitle(getString(R.string.app_name))
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int random = (int) ((new Date().getTime() / 1000L) % Integer.MAX_VALUE);
notificationManager.notify(random, notificationBuilder.build());
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vishesh.test">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".MnetApplication"
android:allowBackup="true"
android:icon="#drawable/mnet_icon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".activities.LoginActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask"
android:theme="#style/Theme.AppCompat.Light.NoActionBar.FullScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_main"
android:launchMode="singleTask"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".activities.ConversationDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_conversation_detail"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode="singleTask"
android:windowSoftInputMode="stateHidden|adjustResize">
<intent-filter>
<action android:name=".activities.ConversationDetailActivity" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="http" />
</intent-filter>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.ApprovalDetailActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_approval_detail"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NewConversationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_new_conversation"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.NotificationActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/title_activity_notification"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar"
android:windowSoftInputMode="stateHidden|adjustResize">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<activity
android:name=".activities.ProfileActivity"
android:label="#string/title_activity_profile"
android:configChanges="orientation|keyboardHidden|screenSize"
android:parentActivityName=".activities.MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="mnet.mediaware.com.m_net.activities.MainActivity" />
</activity>
<service
android:name=".utils.firebase.MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".utils.firebase.MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
</manifest>
Try Adding android:exported="true" to the activity in AndroidManifest.xml
First finish Login activity, and then call start activity.
LoginActivity.this.finish();
LoginActivity.this.startActivity(intent);
In your manifest, define your class which you want to open like this
<activity
android:name=".activities.ApprovalDetailActivity"
android:exported="true"
android:screenOrientation="portrait"
android:theme="#style/MyMaterialTheme"
android:windowSoftInputMode="stateHidden">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Nothing works for me except this one. The thing work for me is simple. Make sure you add this in the activity that you want to open directly.
<intent-filter>
<action android:name="MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
And from the push notification you must add a new payload: click_action
it will looks like this -
"notification": {
"title": "hello",
"body": "test message",
"click_action": "MAIN_ACTIVITY"
},
Note: You can name it as you want MAIN_ACTIVITY but must be same in both place.

How to Avoid Launching the Android Application Twice ,Running from Eclipse to real device

I am Running the application from eclipse and it's being launched twice: first time launching the app, then again relaunching after few seconds
My app Splash Screen--->> Main activity(Both Opening twice).
i already tried adding android:launchMode="singleInstance" in my manifest file, but not success.
i have tried 3 different applications from my eclipse still opening twice in my Kitkat ,lollipop real device (created new project that one also opening twice)
EDIT 1 :
Tried adding this line in manifest file but not Success-android:launchMode="singleTop"
please let me know How to solve this issue.
manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:largeHeap="true"
android:launchMode="singleInstance"
android:theme="#style/AppTheme2" >
<activity
android:name=".Start"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
My start Activity.java
public class Start extends Activity
{
SessionManagerFor_Signin session;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Session class instance
session = new SessionManagerFor_Signin(getApplicationContext());
// Remove the Title Bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.start);
ImageView Image1=(ImageView)findViewById(R.id.imageView1);
//Animation Bottom to Top
TranslateAnimation animation2 = new TranslateAnimation(0.0f, 0.0f,400.0f, 0.0f);
animation2.setDuration(1000);
animation2.setFillAfter(false);
Image1.startAnimation(animation2);
Thread timer = new Thread()
{
#Override
public void run()
{
try {
sleep(3000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
finally
{
session.checkLogin();
finish();
}
}
};
timer.start();
//For Full Action bar Color Starts
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
}
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.FUllStartColor);
//For Full Action bar Color Ends here
}
#TargetApi(19)
private void setTranslucentStatus(boolean on) {
Window win = getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
if (on) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
}
win.setAttributes(winParams);
}
Try adding launchMode "singleTop" in your Manifest to your activity.
<activity
android:name="MyActivity"
android:launchMode="singleTop"
... >
Apply intent filter only in one of your activity. Remove from MainActivity...
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:largeHeap="true"
android:launchMode="singleInstance"
android:theme="#style/AppTheme2" >
<activity
android:name=".Start"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" >
</activity>
</application>
Remove this from one of the two activities:
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
This intent-filter indicates to Android which is the Main Activity, and you should have one only.
try this:
android:launchMode="singleTask"
May be this will work.
If it does not work then reinstall eclipse.
Apply the below to your splash screen activity,then clean the project and run again..
Try to register activity in manifest file with complete package name.
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

Android app crash: Unable to instantiate activity ComponentInfo

07-18 04:48:22.465: E/AndroidRuntime(19105): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamwli.fa.yearbook/com.liamwli.fa.yearbook.Home}: java.lang.NullPointerException
That is the error I get.
I have defined the Home class in the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liamwli.fa.yearbook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Home"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="com.liamwli.fa.yearbook.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And it only started doing this when I added the putExtras line in my main activity:
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myname = name.getText().toString();
Intent i = new Intent("com.liamwli.fa.yearbook.HOME");
i.putExtra("myname", myname);
startActivity(i);
}
});
So, can anyone please explain what is happening?
use this:
Intent i = new Intent(context,otherActivity.class);
context of that activity from where you want to start activity.otherActivity is the name of activity which you want to start
Try this once
Intent i = new Intent(MainActivity.this,Home.class);
i.putExtra("myname", myname);
startActivity(i);
And I don't think this is required in Manifest
<intent-filter>
<action android:name="com.liamwli.fa.yearbook.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
change like this
Intent i = new Intent(MainActivity.this, Home.class);
i.putExtra("myname", myname);
startActivity(i);
Try this...
Intent i = new Intent( YourActivityName.this , otherActivity.class);
Ok, I looked and it looked like I was setting a variable contents outside of the onCreate method. So that is why it wasn't working.

SearchView in my application doesn't seem to be working

I have implemented the following code but my SearchActivity::OnCreate is not getting called. Can someone please tell me what I'm missing?
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kace.SearchTest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity"/>
<activity
android:name=".Search_testActivity"
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=".SearchActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH"/>
</intent-filter>
<meta-data android:name="android.app.searchable" android:resource="#xml/searchable"/>
</activity>
</application>
</manifest>
/res/xml/searchable.xml:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/description" >
</searchable>
SearchActivity.java:
public class SearchActivity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction()))
{
String query = intent.getStringExtra(SearchManager.QUERY);
int q;
q = 10;
}
}
#Override
protected void onNewIntent(Intent intent)
{
// TODO Auto-generated method stub
super.onNewIntent(intent);
}
}
Android guides explicitly says:
// Assumes current activity is the searchable activity
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
Which is in your case NOT TRUE. Because you don't want to search in MainActivity but in SearchableActivity. That means you should use this ComponentName instead of getComponentName() method:
ComponentName cn = new ComponentName(this, SearchableActivity.class);
searchView.setSearchableInfo(searchManager.getSearchableInfo(cn));
try this :
Update your AndroidManifest.xml as:
<activity
android:name=".Search_testActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity"/>
</activity>
<activity android:name=".SearchActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.SAMPLE_CODE" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#layout/searchable" />
</activity>
</application>
put searchable layout in layout/ instead of /res/xml/:
layout/searchable.xml :
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/description"
android:searchMode="showSearchLabelAsBadge"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
android:voiceLanguageModel="free_form"
android:voicePromptText="#string/search_invoke"
android:searchSuggestSelection=" ? "
/>
For more working example see my answer in this post
And
You can download working example from SearchWidgetExample
Found the answer! In the onCreate method of my main activity (Not the search activity), I added the following lines:
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo = searchManager.getSearchableInfo(getComponentName());
SearchView sv = (SearchView)findViewById(R.id.searchView1);
sv.setSearchableInfo(searchableInfo);
Try adding default_searchable setting under your "source" activity:
<meta-data
android:name="android.app.default_searchable"
android:value=".SearchActivity" />
In short, you need default_searchable setting for the "source" activity and searchable setting for the "search result" activity (your SearchActivity class).

Start an Activity not from an Intent

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>

Categories

Resources