Issues in Adapter to Fragment in intent using web services - android

When i click on a button in recycler view adapter then one popup is appeared from service then it will go to next fragment.i write code like this it shows error how to solve this?
dialog_confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new ConfirmAsyncTask().execute();
Intent intent = new Intent(context,OngoingFragment.class);
v.getContext().startActivity(intent);
}
});

If your are using Activity beside Fragment then add your activity in Manifest file. and if your are using Fragment then use FragmentManager to switch your fragment instead of Intent.

You can not start a Fragment like starting an Activity.
See this.

Related

Android Studio onClick and OnclickListener not working once changed to another Activity

I've added my acivity class in this link
Click MeI am fairly new to Android and am having difficulty trying to add a button on my second activity. I am able to place a button in my main activity and then I use it to navigate to my secondary activity (using setContentView(R.layout.)) and then I use the same 'onClick' method or even 'OnClickListener' method but the button on my second activity just wont work on another activity. Maybe i am missing something
]3
just try to do this:
public class FirstActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
findViewById(R.id.about_us).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this, SecondActivity.class));
}
});
}
}
and in second activity again find your button in second activity xml by id and write onClickListener for it
You need to implement two separate methods for two different buttons. I would suggest do these things in the Java code instead of XML.
You can do some thing like this:
Button button = findViewById(R.something.something);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//perform your operation(s) here.
}
});
As I understand you try to use one layout.xml for both activities.
You need to declare method click1 in both activities, not only in first.
It means that your first activity has to have method public void click1() and the second activity has to duplicate method public void click1()
I know it's old however,
#Meikiem idea is great. When you use setContentView(View View) you are just setting the activity's
content to another view (xml), and thus not really using the other .java file which has another
onClick method defined for the second button.
Activity's setContentView(view)
You need to create an Intent and pass it along the startActivity method.
Intent Definition

How to send intent data back to previous activity when back arrow icon is pressed?

I have an Android app that displays elements in a GridView: each of these elements is clickable and starts an Activity with its details; then you can go through another activity from the second one to add more data.
My question is: when I go back from 3rd to 2nd activity, my app crashes (and I know this is because going from 3rd activity to 2nd one, the 2nd so called hasn't got the intent data that it needs).
What can I do to solve this issue?
My Gridview calling the 2nd activity
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(getApplicationContext(), PokemonDetails.class);
i.putExtra("id", position);
startActivity(i);
}
});
My 2nd activity calling the 3rd:
pokeDetails.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MyPokeDetails.class);
startActivity(i);
}
});
you can just override the onBackPressed() method with the same function of your buttons.
#Override
public void onBackPressed() {
//put Intent to go back here
}
Your activity should be recreated from the last state when you go back to it. Do you check the intent you have your data in in your 2nd activity to be non null? I guess the app could crash because of that.
You could also work with the savedInstanceState.
Override onSaveInstanceState and put the id you need into the bundle. If onCreate of your second Activity gets called, look if Bundle is non null and go get your value.
Further info: https://developer.android.com/training/basics/activity-lifecycle/recreating.html
You should use startActivityForResult() instead of startActivity() For details see this answer and Official documentation

Activity to Fragment on button click

I have navigation bar on my app. Using that has created fragments. I added regular activities to the app and I have found how to go from a Fragment page to an activity page with using the onclick method.
By the click of a button I want to go from an activity page to a fragment page. How do I go about that?
Thank you
apply on click listner on the button and apply below code :-
Fragment fragment = new VehiclesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.container,fragment).commit();
If you want to close the current activity to go back to your Fragment-container Activity you may call the finish() method or onBackPressed() method (in case you're not overriding it).
If you want to bring a fragment to the current activity you must have a fragment container and attach it with:
getSupportFragmentManager().beginTransaction.add(R.id.container, fragment, TAG).addToBackStack().commit();
Try this code..
btn.setOnClickListener(new View.OnClickListener()){
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),FragmentTest.class);
getActivity().startActivity(intent);
}
});
If you want to move around in fragment then you have to be using either navigation bar clicks on tabs or use this from one fragment to another on a click of button .. like this
public void onNavigationDrawerItemSelected(int position) {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = Fragment.instantiate(MainActivity.this, <name of the fragment>);
Bundle bundle = new Bundle();
bundle.putBoolean(ALL_EVENTS, true);
fragment.setArguments(bundle);
fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
Simply use this:
Intent(view.context, DestinationActivity::class.java).also {
view.context.startActivity(it)
}

recyclerview open activity with navbar on click item

I'm new android developper. In my application I have a main activity with toolbar, that contains a title, and a recycler view.
The recycler view contains some items. I want to open a activity on click on them. My code is able to open the activity but the toolbar disappear.
I open the activity like this:
public VHolder(final View itemView){
super(itemView);
title = ((TextView) itemView.findViewById(R.id.articleTitle));
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(itemView.getContext(), ArticleActivity.class);
intent.putExtra("title", currentNews.title);
intent.putExtra("content", currentNews.htmlContent);
itemView.getContext().startActivity(intent);
}
});
}
Have you any ideas and advices ?
Sorry about my poor english ;).
You are doing everything in the correct way. The problem is that toolbar is just another widget on your Activity and cannot be shared between multiple activities. So you should add toolbar view to the layout of the Activity which you are starting (ArticleActivity).
As another option you can show Fragment over RecyclerView instead of starting new Activity. Similar to this: how to open a different fragment on recyclerview OnClick
I have found the cause of my problem of toolbar. The opened activity (ArticleActivity) not extends "AppCompatActivity" like the main activity but, "Activity".

Start another Activity inside the FrameLayout of TabActivity

I am trying to make an application which have 4 tabs at the bottom of the screen.
All of them contain Activity (Intent).
And I want to navigate any of the Activity to another activity. But want to keep the TabWidget visible.
Let me know as quickly as possible if you know about it.
Shaiful
The problem of error occuring due to the replacement of activities can be solved in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
For going from list to details...
(This can be in the onClickListener)
private OnClickListener textListener = new OnClickListener() {
#Override
public void onClick(View v) {
Constants.SHOW_DETAILS = true;
Intent intent = new Intent(context, DetailsActivity.class);
replaceContentView("activity3", intent);
}
};
public void replaceContentView(String id, Intent newIntent) {
View view = ((ActivityGroup) context)
.getLocalActivityManager()
.startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
.getDecorView();
((Activity) context).setContentView(view);
}
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
#Override
public void onBackPressed() {
// TODO Auto-generated method stub
super.onBackPressed();
if (MathHelper.SHOW_DETAILS) {
Log.e("back", "pressed accepted");
Constants.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent intent = new Intent(this, Tab_widget.class);
startActivity(intent);
finish();
}
}
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
tabHost.setCurrentTab(Constants.LIST_ACTIVITY);
This is implemented in Tabs with multiple activities in a single tab.
However when multiple times activities are called StackOverFlow error arises. Tried very hard but unable to solve it.. Please someone tell a method to solve this problem
Also need to Replace an activity in a tab, However from child activity. How is that to be done?
At any one moment there may only be one activity. Docs about this here

Categories

Resources