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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} | |
} |
Create a class Fragment and name it as NameFragment.java, Here is the code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
Create a new layout name it as activity_for_fragment_name.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Calling Fragment NameFragment from MainActivity, you can do it look like the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Intent intent = new Intent(MainActivity.this, ActForFragmentNameActivity.class); | |
startActivity(intent); |
2. Without using Class Activity
If you create fragment without create its Activity, simple do like the following code :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Intent intent = new Intent(getActivity(), TujuanActivity.class); | |
getActivity().startActivity(intent); |
.... 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.
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