How to connect two activities to make a program? - java

I am a beginner in java.
I want to make sure to link my second main activity on my first activity.
I try to put a line of code in my first activity but that does not connect anything at all.
I put the line of code:
Intent intent = new Intent(this, DeuxiemeActivity.class);
startActivity(intent);
finish();
Thanks,

Inside AndroidManifest.xml,
Change
<activity android:name=".MainActivity">
to this,
<activity android:name=".MainActivity" android:noHistory="true">
noHistory="true" and finish() both are similar.
But, When we start a new activity, It will be closed at the same time. So using noHistory="true" is better than using finish();.

Related

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

Launch a new screen (activity) instead of replacing the crurent one

My first App
This is my first android app - QR Code Scanner using ZXingScanner Library.
Is there any way to launch a new screen (activity) instead of replacing the crurent one, so i can press back from the scanner to go back to main screen where is the button?
Please help me. Thanks all
I think that this site can help you: http://developer.android.com/training/basics/firstapp/starting-activity.html
In short words:
You need to create new
Intent myIntent = new Intent(this, SecondActivity.class) and than start it by startActivity(myIntent).
Additionally it will be good to define your MAIN activity in AndroidManifest.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
You should create 2 activities in your app.
From first activity send an intent to the second activity that you created before (I think that you can create one). With sending an intent to another activity the first one remain, and you can use back button to load that.
Now this is the easiest way that you can send an intent:
Intent intent= new Intent(Activity1.this,Activity2.class);
startActivity(intent);
But there is some other ways to send an activity(similar to this one):
How to start new activity on button click

Android, bring already started activity back to front

My android application has multiple activities. Each activity has a 'Back to Home' button. For example, Main activity opens sub-activity A, sub-activity A opens sub-activity B.
On the screen of sub-activity B, there's a 'Back to Home' button which is supposed to bring back the Main activity into visibility.
I've tried the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT but no use.
This is a part of my AndroidManifest.xml:
...
<activity android:name="vn.agritrade.Main_Activity"
android:label="Agritrade"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
android:uiOptions="splitActionBarWhenNarrow"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
...
This is how I relaunch the Main activity:
/**
* Relaunch main activity (called by 'Back to Home' button)
*/
public void open_main_activity(View view) {
Intent intent = new Intent(this,Main_Activity.class);
intent.putExtra(DEFAULT_EXTRA,"");
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
}
What else should I need?
Use below code it may help,
Intent intentHome = new Intent(getApplicationContext(), MainActivity.class);
intentHome.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentHome.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.putExtra(DEFAULT_EXTRA,"");
startActivity(intentHome);
It is worked for me.
Don't forget to mark as answer it is help to you.
Intent.FLAG_ACTIVITY_CLEAR_TOP may solve your problem
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Intent intent = new Intent(this,Main_Activity.class);
intent.putExtra(DEFAULT_EXTRA,"");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
My final solution is:
Keep the main activity stay open
Each time I need to turn back to main screen: close all sub-activities

Launching Intent cannot find Activity

My program has a list of tabs that each contain a list of people. Clicking a person should edit their details, but instead, clicking a person throws an ActivityNotFoundException. My code that starts the edit activity:
Intent intent = new Intent("my.package.EDIT"); // Person is both
intent.putExtra("my.package.PERSON", (Parcelable) myPerson); // Parcelable and
GroupArrayAdapter.this.getContext().startActivity(intent); // Serializable
Relevant part of AndroidManifest.xml:
<activity android:name="EditActivity">
<intent-filter>
<action android:name="my.package.EDIT" />
</intent-filter>
</activity>
What's going on?
There's two ways to start an Activity, either explicitly or implicitly. Explicitly means that you use the class name directly; usually if you're calling an Activity that is within your own application, what you mean to use is the explicit method because it's far easier:
Intent intent = new Intent(context, EditActivity.class);
context.startActivity(intent);
However, if you really do mean to use implicit filtering, then your intent filter is missing something. Every implicit intent filter requires the category android.intent.category.DEFAULT (with few exceptions). Try this and see if it works:
<activity android:name="EditActivity">
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="my.package.EDIT" />
</intent-filter>
</activity>
Suggested reading: intent filters.
When you instantiate your Adapter, pass the context from the calling activity. Then in your adapter use context.startActivity(...);

Restore android app stack from background

Lets say I launch my app from the home screen, navigate through some activities, then I press the home key and do something else in the Gmail app.
After i'm done checking my mail,I press the home key again to leave the Gmail app and click my app's icon at the home screen again to return to it.
When I return to my app, I want it to return to the last activity I was at, NOT start a whole new session. I've been trying to figure this out all day.
My manifest for my first activity is as follows:
<activity android:name=".Main"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:alwaysRetainTaskState="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
The category attribute LAUNCHER makes my app ALWAYS start at activity Main, so I don't know how to go about restoring the last activity. People have told me to use sharedpreferences to save the last activity and load it on Launch, but I don't think it's intended to be done that way because it isn't very elegant.
I think its the only way because what happens when you're launching an app is that Launcher application sends intent "android.intent.action.MAIN" And the only activity in your app that responds to this intent is your main activity, thus it gets activated. So the only thing you can do is save somewhere your session and on activity start up if there's already saved session restore the app to the previous state.
Try using one of these in your manifest :
<activity android:launchMode=["multiple" | "singleTop" |
"singleTask" | "singleInstance"] ...
Are onResume() and onPause implemented properly?
protected void onResume(){
super.onResume();
}
protected void onPause() {
super.onPause();
}

Categories

Resources