How to limit my main activity to not go back on the last page of my Fragment once return from the last page of Fragment to Main activity like I have One Home activity and that home activity leads to 3 more activities on clicking the button and in which one activity has fragments. The last page of the fragment is leading to home activity but when I back pressed on the home activity it again leads me to the last page of a fragment from where I've come and then I press the back button again then my application shuts
MY MAIN ACTIVITY CLASS
public class Home extends AppCompatActivity {
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
}
public void Consultation_Form(View view) {
startActivity(new Intent(getApplicationContext(), ConsultationForm.class));
finish();
}
public void Registration_Form(View view) {
startActivity(new Intent(getApplicationContext(), RegistrationForm.class));
finish();
}
public void Assessment_Form(View view) {
startActivity(new Intent(getApplicationContext(), AssessmentForn.class));
finish();
}
public void Booking_Form(View view) {
startActivity(new Intent(getApplicationContext(), BookingForm.class));
finish();
}
}
MY FRAGMENT CLASS OF VIEWPAGER
public class PC_v16 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.pc_v16, container, false);
TextView tv = (TextView) view.findViewById(R.id.tvReferral);
Button btBackHome = view.findViewById(R.id.btBackHome);
btBackHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Home.class);
getActivity().startActivity(intent);
}
});
return view;
}
}
You just need to finish your Viewpager Activity before moving to Home Activity.
In your Fragment
Change this
btBackHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Home.class);
getActivity().startActivity(intent);
}
});
To
btBackHome.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Home.class);
getActivity().startActivity(intent);
getActivity().finish();
}
});
Related
Here is my code
med=(LinearLayout) getView().findViewById(R.id.second);
med.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i= new Intent(getContext(),SecondActivity.class);
i.putExtra("table_name","questCompFunda");
startActivity(i);
overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}
});
I see that you are trying to use
overridePendingTransition in a fragment
So the correct way would be,
getActivity().overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
Edit:
Launching an activity from cardview
cardview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
Intent intent = new Intent(getActivity(), SomeActivity.class);
startActivity(intent);
getActivity(). overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
}
});
From the first activity I would like to go to the second, then from the second to the third. In the third activity I would like to enter the name in EditText, then after pressing the button, go to the first activity and at the same time send the information entered in the third activity.
Unfortunately, after pressing the button in the third activity, instead of returning to the first activity, I return to the second activity. Was the first activity killed? What could I do to ensure that the information is correct for the first activity? This is my code:
First:
public class MainActivity extends AppCompatActivity {
TextView textViewInformation;
Button button_GoToSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textViewInformation = findViewById(R.id.textView);
button_GoToSecond = findViewById(R.id.button);
button_GoToSecond.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(MainActivity.this, Second.class);
startActivity(i);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
String name = i.getStringExtra("name");
textViewInformation.setText(name);
}
}
}
Second:
public class Second extends AppCompatActivity {
Button button_GoToThird;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button_GoToThird = findViewById(R.id.button2);
button_GoToThird.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Second.this, Third.class);
startActivity(i);
}
});
}
}
Third:
public class Third extends AppCompatActivity {
EditText editText_Data;
Button button_SendData;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
editText_Data = findViewById(R.id.editText);
button_SendData = findViewById(R.id.button3);
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
}
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish();
}
}
just remove finish(); thats it
The reason that it goes to the second activity is because of this:
button_SendData.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
i.putExtra("name", name);
setResult(RESULT_OK, i);
super.finish(); //This kills the current activity.
}
You should do:
public void finish() {
String name;
name = editText_Data.getText().toString();
Intent i = new Intent(Third.this, MainActivity.class);
Bundle bundle = new Bundle();
bundle.putString("name", name);
startActivityForResult(i, RESULT_OK, bundle);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent i) {
if((requestCode == 1) &&(resultCode == RESULT_OK)) {
Bundle bundle = i.getExtras();
String name = bundle.getString("name");
textViewInformation.setText(name);
}
}
When you call finish, it just kills the current activity. If you want to go back to the first activity, just start a new activity for the first activity and pass the data in a Bundle.
If you want to have more of a stack concept, you can use Fragments.
Currently I have a fragment_one.xml which has 5 CardViews on it and and each card has a Button on it that is meant to go to separate XML pages (Lesson_One,Lesson_Two etc...) but with the code I have in OneFragment.java, both buttons are opening Lesson_Two
How I can I fix this? Here is my code
FragmentOne.java
public class OneFragment extends Fragment{
Intent intent;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
intent = new Intent(getActivity(), LessonOne.class);
final Button button = (Button) root.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
intent = new Intent(getActivity(), LessonTwo.class);
final Button button2 = (Button) root.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(intent);
}
});
return root;
}
}
You're assigning intent twice, effectively overwriting the first intent with the second.
So no matter which click event is triggered LessonTwo.class is the activity that is started.
A simple fix would be to create the intents inside the click handlers like
public class OneFragment extends Fragment{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
final Button button = (Button) root.findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getActivity(), LessonOne.class));
}
});
final Button button2 = (Button) root.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(getActivity(), LessonTwo.class););
}
});
return root;
}
}
This makes it explicit which click handler starts what activity
Alternative answer - implement the click listener on the class itself.
This cleans up the onCreateView method. You also don't need to "capture" the buttons to set their listeners.
public class OneFragment extends Fragment implements View.OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_one, container, false);
root.findViewById(R.id.button1).setOnClickListener(this);
root.findViewById(R.id.button2).setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
Class clz = null;
switch (v.getId()) {
case R.id.button1:
clz = LessonOne.class;
case R.id.button2;
clz = LessonTwo.class;
}
if (clz != null) startActivity(new Intent(getActivity(), clz));
}
}
public class FullscreenActivity extends AppCompatActivity {
private ImageButton act;
private ImageButton sat;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
act = (ImageButton) findViewById(R.id.act);
sat = (ImageButton) findViewById(R.id.sat);
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
}}
What am I doing worng. I have a main UI with six imagebuttons, each one linking to a different activity. How can I link then all in the main activity which is called fullscreen activity
I think you want to bring the FullScreenActivity to front so you only have to finish the front Activity because you didn't finish it
just do finish(); on your sat.class or act.class
or....
You can do this too on your sat.class or act.class:
Intent i = new Intent(this, FullScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
why is the above part inside some other button's click listener? Move it outside the anonymous inner method like
sat.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, sat.class);
startActivity(intent);
}
});
act.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v) {
Intent intent = new Intent(FullscreenActivity.this, act.class);
startActivity(intent);
}
});
I am trying to make a mobile application,but when ever i try to press the button i get a crash. The button should take me to a new activity page, i have already connect the other pages with the button in a right way. i am just wondering if this code is correct or not:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v)
{
Button a1= (Button) findViewById(R.id.button);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
}
public void onButtonClick1(View c)
{
Button a1= (Button) findViewById(R.id.button2);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ThirdPage.class);
startActivity(intent);
}
});
}
public void onButtonClick2(View d)
{
Button a1= (Button) findViewById(R.id.button3);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FourthPage.class);
startActivity(intent);
}
});
}
public void onButtonClick3(View f)
{
Button a1= (Button) findViewById(R.id.button4);
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FifthPage.class);
startActivity(intent);
}
});
}
}
i just learned java from some videos in the youtube,so i am not sure if i did the activity function well or not. Thank you
There can be few reasons after it
1) Make sure you defined activity in Manifest file
2) check for android:onClick="" in xml file..
OR
Try Binding Buttons in onCreate() method.
and You can simply use this for onClick
a1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
Remove onClick attribute in your activity_main.xml file and copy and past this code in your MainActivity.java file
public class MainActivity extends AppCompatActivity {
Button b1,b2,b3,b4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1= (Button) findViewById(R.id.button);
b2= (Button) findViewById(R.id.button2);
b3= (Button) findViewById(R.id.button3);
b4= (Button) findViewById(R.id.button4);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, SecondPage.class);
startActivity(intent);
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, ThirdPage.class);
startActivity(intent);
}
});
b3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FourthPage.class);
startActivity(intent);
}
});
b4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this, FifthPage.class);
startActivity(intent);
}
});
}
}
If you are using the onclick attribute in layout XML then you doesn't need to set the onclick listener. If you are not using onclick then you need to set click listener. In your case your case, I think use are using onclick attribute in layout XML and also trying to set the click listener in using java code. so you need to either use onclick attribute in layout XML or use setOnCliickListener() in java code