Intent is not working for moving fragment to activity - java

I'm having trouble moving intent class fragment to activity. I'm trying to send intent from one Fragment to another Activity.
Here is the code:
Intent intent = new Intent(getActivity(), MainActivity.class);
getActivity().startActivity(intent);
Here is my Manifest File
<activity android:name=".activities.MainActivity"
android:supportsPictureInPicture="true"
android:resizeableActivity="true"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustNothing"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"/>
but it's not working for me and on another side, I have done some RND but all are same for me.
How can I Solve the issue
Thanks In Advance.

remove this android:launchMode="singleTask"
which looks like this
<activity android:name=".activities.MainActivity"
android:supportsPictureInPicture="true"
android:resizeableActivity="true"
android:windowSoftInputMode="adjustNothing"
android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"/>
if your current activity is mainactivity and you are launching it again , it will not work because launchmode is set to single task in manifest
Solution :- remove this tag
android:launchMode="singleTask"

Related

Shows only black screen when moving to another activity

I don't know what I've done, but after I restart the Android Studio and when i run the app, it just shows only black screen when I'm calling new activity from previous activity.
The app is running good before I restarted the Android Studio.
I don't find where the wrong code is. There is no error based on the IDE.
I have this in onCreate
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
I have launched the activity by this code:
context.startActivity(new Intent(context, homeActivity.class));
in the previous class
I've seen many answers here, but do not solved my problem.
EDIT
Screenshot after calling the homeActivity
Follow CamelCase for naming conventions for Java classes check homeActivity.class --> HomeActivity.class
Ensure you define homeActivity.class in your manifest.xml
<application
android:allowBackup="true"
android:supportsRtl="true"
android:installLocation="auto"
tools:replace="android:supportsRtl"
android:icon="#mipmap/app_icon"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#style/AppTheme" >
// ... Other activity
<activity
android:name=".HomeActivity"
android:screenOrientation="portrait">
// ... Other meta-data & services.
</application>
Try create another activity with the same layout and class, but name it by the big letter.

Prevent Android system from resuming task if the new Intent is the same with initial Intent that launches the app

This question is related to deep link. For example, I have four activities in my app, namely MainActivity, FirstActivity, SecondActivity, and DeepLinkActivity. Here are the activities in the manifest file.
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FirstActivity" />
<activity android:name=".SecondActivity" />
<activity android:name=".DeepLinkActivity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="launch" />
</intent-filter>
</activity>
DeepLinkActivity will launch whether FirstActivity, SecondActivity, or MainActivity depending on the URI received:
public class DeepLinkActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
launchIntent(getIntent());
}
private void launchIntent(Intent intent) {
Intent newIntent = null;
switch (intent.getData().getHost()) {
case "first":
newIntent = new Intent(this, FirstActivity.class);
break;
case "second":
newIntent = new Intent(this, SecondActivity.class);
break;
default:
newIntent = new Intent(this, MainActivity.class);
break;
}
startActivity(newIntent);
finish();
}
}
The problem is, Android system seems to record the initial Intent that launched the app, then uses it to resume whatever Activity is currently running in the foreground -if- the new Intent is the same with initial Intent.
Consider the following case:
User click launch://first deep link → FirstActivity is opened.
User minimize the app then click launch://second → SecondActivity is opened on top of FirstActivity.
User minimize the app again then click launch://first.
After step 3, what I expect is the OS open FirstActivity. But actually it resumes the task, so SecondActivity (from step 2) reappears. This happens because the deep link on step 1 and 3 is the same, hence the OS regards them as the same Intent.
How can I prevent the Android system from resuming task in this particular case?
After some trials this is what I did to get it working.
Setting the Flags for the newIntent before starting it:
newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
However, setting the flags alone didn't do the job, the app is still resumed to the last activity. Then I add the launchMode to DeepLinkActivity:
<activity android:name=".DeepLinkActivity"
android:theme="#android:style/Theme.NoDisplay"
android:launchMode="singleTask">
Now the the app is restarted and open the expected activity! Note that the launchMode can be singleInstance as well.
You can try defining something on the Activities OnPause function, to reset the intent every time the activity is paused.

Android notification intent creates new instance instead of opening existing instance

If I click my notification while the app is minimized it opens the app and the UI is reset. I would like the notification to open the existing instance. I've been trying to make it use the existing instance.
This is my notification code:
mBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Title")
.setContentText("Tuned in to " + radiostation);
Intent resultIntent = new Intent(getApplicationContext(), RadioChooser.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP |
Intent.FLAG_ACTIVITY_NEW_TASK);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(getApplicationContext());
stackBuilder.addParentStack(RadioChooser.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getApplicationContext(), 0,
resultIntent, 0
);
mBuilder.setContentIntent(resultPendingIntent);
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mBuilder.setOngoing(true);
mNotificationManager.notify(1, mBuilder.build());
AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".RadioChooser"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:configChanges="orientation|keyboardHidden" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:enabled="true" android:name=".MusicPlayer" />
</application>
Well, nomatter how you try to do this, the Activity will most likely be restarted if it has been minimized(sometimes it is not though). You need to prepare for it to ALWAYS be restarted. The way to go about this is, in our onSaveInstanceState() method, store all of the UI related variables. Then, either in onCreate or onRestoreInstanceState(), re-initialize those variables and build your UI back up from their value. So, if you use fragments to display different pages of your app, when the main activity is stopped and onSaveInstance() is called, store the name or number or some identifier of which fragment it was in the Bundle object that onSaveInstance() passes. When the activity is recreated, your code should take those variables from the bundle passed in onCreate(), and use them to display the correct fragment, with the correct information.

Combining Modules Android Application

I'm doing an Android project on Eclipse and I have 2 independent apps sources(Modules). How do I combine these two modules, such that when a button (present on first app) is clicked, the second app is launched?
As far I've just created the button and that's it.Any Help would be appreciated.
As I'm a beginner, please be specific :) Thank you!
In the manifest of the second app :
<activity
android:name=".MainActivitySecondApp"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.external.intentReceiver" />
</intent-filter>
</activity>
And then place the following code inside the onClickListener of the Button in the first App :
Intent intent = new Intent();
intent.setAction("com.external.intentReceiver");
context.startActivity(intent);
So, when the button is clicked, an intent of the type com.external.intentReceiver will be fired. The MainActivitySecondApp will then open since it is meant to handle such intents as defined in the Manifest.

Go back to the First/Main activity without reloading it

I am developing an android app. I need to call my MainActivity without reloading it as it has huge amount of data fetch from internet.
Suppose, I am on third activity now and I want to go back to MainActivity.
If I use:
Intent i = new Intent(Third.this,Main.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
it will load MainActivity but I don't want to reload it. like from Second Activity I call finish() and it does exactly want i need.
This is how to do it:
Intent i = new Intent(this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(i);
metntion it in your AndroidManifest.xml file
<activity android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation">
do nothing inside the method onResume() and onstart() when coming back to this activity
and try intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); instead of addFlags() method
In Third activity when you want to go to first activity, put finish() mwthod there.
In Second activity, after onCreate() method, put
#Override
public void onResume(){
super.onResume();
finish();
}
I think this code will work for you, jus try it.
Try adding this to your manifest:
android:configChanges="keyboard|keyboardHidden|orientation">
add this line to the activity in your manifest like this:
<activity android:name=".Main"
android:label="#string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Categories