Open a fragment of an activity from another activity
Asked Answered
J

2

7

Hi All I want to open the "Text-To-Speech output" fragment of Settings from my application. I think first I need to open the settings activity and then its fragment. I tried setting the ComponentName but it was unable to locate the activity.

Should I use FragmentManager; I couldn't find anything specific to my needs. Can somebody give me some link which might explain it well.

Jagatai answered 30/5, 2013 at 12:37 Comment(0)
R
4

You can use the following:

Intent ttsSettings = new Intent("com.android.settings.TTS_SETTINGS");
ttsSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ttsSettings);
Rollo answered 30/5, 2013 at 13:2 Comment(3)
I wish I could buy a beer for you..thanks buddy..:) P.S. I have a release today and I was involved in other task also. And it will open up at a shortcut in Launcher.Jagatai
If you are using a sections pager adapter please see my answer here in detail https://mcmap.net/q/1476117/-how-to-open-or-launch-a-fragment-from-another-activityMatias
This used to work for me perfectly 10 years ago. Now, after I imported my project from Eclipse/ADT to Android Studio (2021.3.1.17), I am getting: "Class referenced in the manifest, com.android.settings.TTS_SETTINGS , was not found in the project or the libraries". Any idea how to solve this?Mccafferty
C
16

You are right, First You need to start the Activity than set the current Fragment in FragmentPager / Manager... Their is no such way to start some foreign fragment from your Activity that would be dangerous see that will lead to zombie fragments floating around the App (or May be I am not aware of that..)

  1. You call the Activity Intent with some parameter for the Fragment name, you want to start i.e. interger, boolean etc...

      Intent intent = new Intent(this,SecondActivity.class);
      intent.putExtra("fragmentNumber",1); //for example    
      startActivity(intent);
    
  2. You check the passed value inside OnCreate of the Second Acitivty and set the desired fragment on top.. inside OnCreate

     if(getIntent().getIntExtra("fragmentNumber",0)==1){
       //set the desired fragment as current fragment to fragment pager
      }
    

However, I am not getting the problem "It was unable to locate the activity." Have you entered the Activity in manifest file than what was the problem you were facing? Please post the full stack trace.

Covington answered 30/5, 2013 at 13:3 Comment(1)
Which flag you want to use with the Intent highly depends upon your dealing with Activity in your Activity Stack.. For the best suitable flag for your requirement see developer.android.com/reference/android/content/Intent.html...Covington
R
4

You can use the following:

Intent ttsSettings = new Intent("com.android.settings.TTS_SETTINGS");
ttsSettings.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(ttsSettings);
Rollo answered 30/5, 2013 at 13:2 Comment(3)
I wish I could buy a beer for you..thanks buddy..:) P.S. I have a release today and I was involved in other task also. And it will open up at a shortcut in Launcher.Jagatai
If you are using a sections pager adapter please see my answer here in detail https://mcmap.net/q/1476117/-how-to-open-or-launch-a-fragment-from-another-activityMatias
This used to work for me perfectly 10 years ago. Now, after I imported my project from Eclipse/ADT to Android Studio (2021.3.1.17), I am getting: "Class referenced in the manifest, com.android.settings.TTS_SETTINGS , was not found in the project or the libraries". Any idea how to solve this?Mccafferty

© 2022 - 2024 — McMap. All rights reserved.