Fragments - How to - android

I have three fragments in two activities;fragmentA in activityA and fragmentB,fragmentC in activityB.when i click a button in fragmentA i want to go specifically to fragmentB (not fragment c) in activityB. please help me in this regard.

add this in OnCreateView of Fragment A:
Button button = v.findViewById(R.id.mybutton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ActivityB.class);
startActivity(intent);
}
});

Related

how to disable the previous activity when click on the next activity

I am having multiple activity in my program the flow of it is like MainActivity-LoginActivity-NextLoginActivity etc.,I am applying transparency theme on each activity except main activity so I want when I click any activity it should show MainActivity in background rather than previous activity.
Maybe you want result below?
You open app, app into these activity in order: MainActivity -> LoginActivity -> OtherActivity
And you want to click a button that in OtherActivity, you don't want to return the LoginActivity but MainActivity
If it is, you can do this:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}
});
In this line: intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Activity will use the Launch Mode by singleTask.
If activity stack has MainActivity, it will finish other activity that on top of MainActivity, then return MainActivity.
You can try to give an id to the parent layout in each activity and handle clicks on it.
Something like:
LinearLayout LoginActivity = (LinearLayout) findViewById(R.id.login_activity);
LoginActivity.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(this, MainActivity));
//If you want to close this one
//this.finish();
}
});

From one activity to two other

Is it possible for one Activity to have two others. What I mean is I have 2 buttons for two parent activities - Button 1 and Button 2. When I click on Button 1 it is opening activity 1 which also has activity 2. When I click Button 2 it is opening same activity 1 like on Button 1 and here is the catch. I want 3rd activity of button 2 to be different. Currently it opens 3rd activity which is on button 1. I will try to illustrate it
Button 1(Activity 1) -> listView (Activity 2) -> TextView (Activity 3)
Button 2(Activity 1) -> listView (Activity 2) -> TextView (Activity 3) <- this must be different from above activity 3
I use same Activity 2 for both menu buttons and then activity 3 must be different for both. I cansee from where this problem come. Because second activity on button 2 is the second activity on button 1. But I don't want to make another activity and to load same thing and to duplicate code/activities. Or should I?
Update:
Button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
intent.putExtra("button", "button1");
startActivity(intent);
}
});
Button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, Menu.class);
intent.putExtra("button", "button2");
startActivity(intent);
}
});
Here is activity 3 for button1
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3);
Bundle b = getIntent().getExtras();
if (b != null) {
String button = b.getString("button");
}
}
Here is activity 3 for button2
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act3_1);
Bundle b = getIntent().getExtras();
if (b != null) {
String button = b.getString("button");
}
}
Use this for button 1 onCLick :
intent.putExtra("button", "button1");
For button 2 onCLick:
intent.putExtra("button", "button2");
Pass that value through from Activity 1 to Activity 3, through Activity 2.
In Activity 3's onCreate, use :
Bundle b = getIntent().getExtras();
if(b != null)
String btn = b.getString("button");
You can use this String to know, whether its from button1 or button2.

How to call another screens from one screen that have seven buttons

Please help me.
I have eight activities.
Suppose that A1 is first activity.
A1 has seven button.
i want to call other activities by clicking buttons From A1.
button2 = (Button) findViewById(R.id.Home_Page);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(activity1.this, activity2.class);
startActivity(i);
finish();
}
});
you have to insert intent in every button and then give the class path like I did..

setcontent view not working

button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
setContentView(R.layout.activity_chart);
}
});
Hi i have the above code wherein upon clicking a button i am trying to display them activity activity_chart. In that activity i want to display a graph. Here i am calling a method createIntent(). But my problem is that the graph is not getting plotted. Please help i am new to android.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
createIntent();
}
public Intent createIntent()
{
...
}
Am i calling the method right.
A new Activity is called with:
startActivity(new Intent(currentActivity.this, nextActivity.class));
Then in your new Activities onCreate(Bundle savedInstance) method you can call setContentView(Layout layout); to set the new Layout.
So if you want to change the Activity when clicking on a Button you have to do the following:
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
startActivity(new Intent(currentActivity.this, nextActivity.class));
}
});
You are currently only changing the layout of the current Activity when clicking the button and not changing to another Activity.
I hope I understood you correctly. If not then provide me with some more code so I can try to understand what you want to do.

How do you make a new activity start when a button is clicked?

I already know how to start a one new activity when you click a button, but I have three buttons on the one layout. And I want each of the three buttons on that ONE activity to link to three other activities.
I have on the activity I have called 'main', Button 1 which is called services and I want to link it to the services activity. Button 2 which is called Search and I want it to go to the Search activity and thirdly 'map' which I want to link to the map activity.
Can someone help me do this please? Thanks
EDIT:Also, I'm a beginner with Android coding, could you explain in a little bit more detail please?
So where is problem? Just set that your class will implements View.OnClickListener and override onClick() method a you got it.
public class MainActivity extends Activity implements View.OnClickListener {
// body
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.serviceBtn:
Intent serviceIntent = new Intent(this, ServiceActivity.class);
startActivity(serviceIntent);
break;
case R.id.searchBtn:
Intent searchIntent = new Intent(this, SearchActivity.class);
startActivity(searchIntent);
break;
case R.id.mapBtn:
Intent mapIntent = new Intent(this, MapActivity.class);
startActivity(mapIntent);
break;
}
}
Tie an onclick listener to all three buttons. In the listener, retrieve the ID of the button. Make a variable of type Class. Depending on the value of the button ID, initialize it to the class of the activity to invoke. Then construct an Intent for that class, and call startActivity().
EDIT for hawaii.five-0: here's how I'd do it:
#Override
public void onClick(View view) {
Class c = null;
switch (view.getId()) {
case R.id.serviceBtn:
c = ServiceActivity.class;
break;
case R.id.searchBtn:
c = SearchActivity.class;
break;
case R.id.mapBtn:
c = MapActivity.class;
break;
}
Intent i = new Intent(YourActivity.this, c);
startActivity(i);
}
EDIT2:
class CurrentActivity extends Activity
implements OnClickListener
{
void onCreate(Bundle b)
{
//Other initialization goes here...
((Button)findViewById(R.id.MyButton1)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton2)).setOnClickListener(this);
((Button)findViewById(R.id.MyButton3)).setOnClickListener(this);
}
}
Its very easy dear. Just put this code on button click event
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
CurrentActivity.this.startActivity(myIntent);
which is very easy to start a new activity when the button is clicked. I have given the example for this.
// R.id.btnAdd -> which is present in your layout page
Button btnStart = (Button) findViewById(R.id.btnAdd); // declare button
// declare listener evernt for button
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
// declare the Intent for moving another activity
Intent view = new Intent(YourCurrentClassName.this,
anotherClassName.class);
// startActivity is used to navigating the view
startActivity(view);
}
};
// set the listener evernt to button
btnStart.setOnClickListener(listener);

Categories

Resources