I have an activity with a toolbar and I want to launch a settings fragment
but I don't want that this fragment will be launched from an activity that
is not the principal activity. So I only can launch SettingsFragment from
my main activity.
My problem is when the SettingsFragment is launched I lose the toolbar that
it was in my main activity and I want to have the toolbar visible with a
back button to return to my activity view in my SettingsFragment, and
launch SettingsFragment from a new activity like SettingsActivity extends
AppCompatActivity is not a solution for several reasons because I need that
the main activity keeps executing always due to the fact that I have a
device connected by USB and libraries for this don't take in acount that
activity will be stopped.
My code is the next:
Code in main activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
...
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
...
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
...
if (id == R.id.action_config) {
getFragmentManager().beginTransaction().replace(android.R.id.content,
new SettingsFragment()).addToBackStack(null).commit();
}
return super.onOptionsItemSelected(item);
}
Fragment code is the next:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.settingsmenu);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
View view = super.onCreateView(inflater, container, savedInstanceState);
view.setBackgroundColor(getResources().getColor(android.R.color.white));
return view;
}
}
XML for the settingsfragment:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_calib_title"
android:key="pref_key_calib_settings">
<EditTextPreference
android:inputType="numberDecimal"
android:title="Emisividad"
android:key="emisividad"
android:defaultValue="0.97"
android:summary="Valor de la emisividad del objeto"/>
<EditTextPreference
android:inputType="numberDecimal"
android:title="Temperatura ambiente"
android:key="TempAmbiente"
android:summary="Temperatura aproximada del ambiente"/>
</PreferenceCategory>
<PreferenceCategory
....
</PreferenceCategory>
And xml for my main activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="pr.sh2.VentanaPrincipal">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="@style/AppTheme.AppBarOverlay"
android:weightSum="1"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:keepScreenOn="true"
android:src="@drawable/flironedesconectada"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_gravity="center_horizontal"
android:background="?attr/colorPrimary">
<ImageButton
android:layout_width="56dp"
android:layout_height="wrap_content"
android:id="@+id/imageButton2"
android:src="@drawable/camerabutton"
android:background="?android:selectableItemBackground"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/imageButton" />
<ImageButton
android:layout_width="56dp"
android:layout_height="wrap_content"
android:id="@+id/imageButton3"
android:src="@drawable/manual"
android:background="?android:selectableItemBackground"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/imageButton2" />
<ImageButton
android:layout_width="56dp"
android:layout_height="wrap_content"
android:id="@+id/imageButton4"
android:src="@drawable/bat_level_unknow_button"
android:background="?android:selectableItemBackground"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEMPERATURA: Desconocida"
android:id="@+id/textView"
android:layout_alignTop="@+id/imageButton2"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:gravity="center"
android:layout_toStartOf="@+id/imageButton3" />
<ImageButton
android:layout_width="56dp"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:background="?android:selectableItemBackground"
android:src="@drawable/grab_record"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/imageButton4" />
</RelativeLayout>
</LinearLayout>
What Can I do? Maybe it is impossible to do in android?
Thanks in advance.
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/f533d0e1-1ad6-4c53-bbcd-d6af40c67840%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.