Reputation: 190
I want to open a fragment when an activity's actionbar icon is clicked and remove the fragment (and go back to activity) when back button (in fragment's layout) is clicked. The imp thing is there is no <fragment>
in my activity layout.
main activity layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is main activity layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main_actions, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.settings:
newintent();
default:
break;
}
return true;
}
private void newintent() {
// Do something here to open FragmentOne.class
}
}
Upvotes: 1
Views: 1437
Reputation: 2188
create a fragment and add this code in your click event method
private Fragment newFragment=new NewFragment();//global variable
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.popBackStack();
FragmentTransaction ft =fragmentManager.beginTransaction();
ft.replace(R.id.current_layout, newFragment,"Order");
ft.commit();
you can follow this link .. click
Upvotes: 2