Reputation: 1568
I am new to fragment concept. In my app I have to save user preferences. I have gone through this doc.
Prepared my preferences xml file and PreferenceFragment file. Everything is fine up to now.
My problem is, I have to add the following code in my onCreate()
method of my MainActivity
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
It is showing on Main screen. But I want to launch this on a button click method
onSettingsClicked(){
// launch preferces screen
}
And I want to display it as a separate screen. How can I do that?
Upvotes: 0
Views: 1661
Reputation: 69
Instead of using fragementTransaction you can use preferenceFragement : How do you create Preference Activity and Preference Fragment on Android? by WannaGetHigh
Upvotes: 0
Reputation: 3177
You need to implement the concept of fragmentTransaction
.
You need to do the following things-
Check out this FragmentTransaction Tutorial, it will guide you-
Do the following changes like -
// Code not accurate, may be some syntax error
@Override
public void onCreate()
{
// super and other stuff
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new NewFragment())
.commit();
Button btn = (Button)findViewById(R.id.button01);
btn.setOnClickListener(new OnClickListener(){
@override
public void onClick(View v)
{
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(android.R.id.content, new new SettingsFragment())
.commit();
}
});
}
Upvotes: 1