Thursday, July 27, 2017

Tutorial How to move from Activity to Fragment and Fragment to Activity

For those who learning android programming for the first time, they will learn about android components like Java Class, XML, Activity, Fragment, Adapter, etc.


After that the next step is learn how to move from one Activity to another Activity using Intent. After learn about Activity, that they will learn about how to move from Activity to Fragment, and the Fragment to Activity

Move From Activity To Fragment


On this part, i will show you two ways how to move from Activity to Fragment:

1. Using Activity For Place a Fragment


We call the fragment in Activity's layout. So when you try to access a Fragment, just call the Activity where you set the fragment using Intent.

Here is the example :

Create a XML file name it as activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.putuguna.fragmenttoactivity.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ini adalah Acvity pertama" />
<Button
android:id="@+id/btn_go_to_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go To Fragment"
android:textAllCaps="false"/>
</LinearLayout>
Layout activity_main.xml will used on class MainActivity. Code for class MainActivity.java looks like the follows :

package com.putuguna.fragmenttoactivity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnGoToFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGoToFragment = (Button) findViewById(R.id.btn_go_to_fragment);
btnGoToFragment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, ActForFragmentNameActivity.class);
startActivity(intent);
}
});
}
}
Well, above is a class that we used as the first page when application is opened

Create a class Fragment and name it as NameFragment.java, Here is the code :

package com.putuguna.fragmenttoactivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
* Created by putuguna on 27/07/17.
*/
public class NameFragment extends Fragment {
private Button btnGoToActivityUmur;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_name, container,false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btnGoToActivityUmur = (Button)view.findViewById(R.id.btn_go_to_umur);
btnGoToActivityUmur.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), UmurActivity.class);
getActivity().startActivity(intent);
}
});
}
}
Next create layout for Fragment NameFragment name it as fragment_name.xml, here is the code :


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Anda sudah berada di fragment"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textSize="20sp"/>
<Button
android:id="@+id/btn_go_to_umur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go To Activity Umur"
android:layout_gravity="center"
android:textAllCaps="false"/>
</LinearLayout>
OK, class Fragment already done. So how can we call Fragment NameFragment from MainActivity?

Create a new layout name it as activity_for_fragment_name.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/fragment_name"
class="com.putuguna.fragmenttoactivity.NameFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Take a look at above code, inside tag there is an attribute class, that contains class Fragment. From where the class com.putuguna.fragmenttoactivity.NameFragment? that is the name of class Fragment that we create before, that is NameFragment.

com.putuguna.fragmenttoactivity.NameFragment contains name of package project and name of class.
com.putuguna.fragmenttoactivity : name of package project that we created
NameFragment : name of class Fragment that we created

Then create a java class for Activity NameFragment, name it as ActForFragmentNameActivity.java, the code is looks like the follows :

package com.putuguna.fragmenttoactivity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
/**
* Created by putuguna on 27/07/17.
*/
public class ActForFragmentNameActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_for_fragment_name);
}
}
As we can see there is not special initialize code inside class MainActivity, it just initialize of its layout. But when this class is loaded, by using attribute class inside tagline fragment, then automatically its fragment will loaded


Calling Fragment NameFragment from MainActivity, you can do it look like the following code :

Intent intent = new Intent(MainActivity.this, ActForFragmentNameActivity.class);
startActivity(intent);
view raw Intent.java hosted with ❤ by GitHub
The part of the code above available on MainActivity, inside button onClick

2.  Without using Class Activity


If you create fragment without create its Activity, simple do like the following code :
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);
fragmentTransaction.commit();

Move from Fragment To Activity


If you try access an Activity from a Fragment, simply like the following code :

Intent intent = new Intent(getActivity(), TujuanActivity.class);
getActivity().startActivity(intent);
Yang perlu diperhatikan adalah :

.... new Intent(getActivity(), .....), getActivity() as Activity of Fragment, because on Intent's constructor required Activity and destination class , so we use getActivity() to take fragment's activity.
And also before you do startActivity(), first write getActivity() then startActivity().
That's all. Sorry for worst english. Thank you.

Related Posts

1 komentar so far

Tutorial How To Move From Activity To Fragment And Fragment To Activity - Android Newbie >>>>> Download Now

>>>>> Download Full

Tutorial How To Move From Activity To Fragment And Fragment To Activity - Android Newbie >>>>> Download LINK

>>>>> Download Now

Tutorial How To Move From Activity To Fragment And Fragment To Activity - Android Newbie >>>>> Download Full

>>>>> Download LINK


EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)