I am using TabActivity(for example, TActivity) to create tab and in every tab I am loading instance of same activity(for example, MainActivity).
Through this activity I want to switch another activitity(for example, ChildActivity which will be same for every tab i.e. in every tab we will get instance of this activity) using Button click.
This button is hardcoded in original TabActivity(TActivity).
Now I am able to do up to this. But Now I want to switch between 'MainActivity' and 'ChildActivity' using same Button.
MainActivity:
class MainActivity ...{
onCreate(){
Btn.setOnClickListener(new OnClickListener(--) {
public void onClick(--){
Intent intent = new Intent(getBaseContext(),ChildActivity.class);
replaceContentView("MainActivity", intent);
}});
}//MainActivity
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)) .getDecorView(); this.setContentView(view);
}//replaceContentView
in ChildActivity:
public void hideCurrentActivity(){
Intent intent = new Intent(getBaseContext(),MainActivity.class);
replaceContentView("ChildActivity", intent);
}
public void replaceContentView(String id, Intent newIntent) {
View view = getLocalActivityManager().startActivity(id,newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) .getDecorView(); this.setContentView(view);
}
Ok, now problem is that this code creates fresh activity everytime. So all previous data is getting lost once I switch to previous activity.But i don't want to create fresh activity instead just want to bring old one to front.
Can anybody help?
Thanks in advance.
Related
I'm stuck by a problem and I don't know how too resolve it.
I want to click on my item in my recycler view, and at this click the layout with the recylcer view will be closed, and my mainActivity will be refresh with the data of the item who i have click on.
you start new activity to get Data but here you start a normal intent you need to start Activity For Result like
startActivityForResult(intent,REQUEST_CODE);
or new Way
ActivityResultLauncher<Intent> result = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if(result.getResultCode() == RESULT_OK){
String data = result.getData().getStringExtra("Data");
// here your data come from next Activity
}
}
});
then call result by
result.launch(Intent);
and in other Activity activity
you need to call
setResult(RESULT_OK,Intent);
finish();
this intent hold your data and will return to previous activity with it
After 2 hours in the sauce I have resolve that and I'm very happy.
The problem with the setResult() and the finish() but if you apply this code
((StructureEnFeuActivity)context).setResult(RESULT_OK, intent);
((StructureEnFeuActivity)context).finish();
You will not have the probleme with "static".
So I'm happy.
And thank you for all.
#Override
public void onClick(View v) {
int i = v.getId();
if (i==R.id.cars){
Intent open_adapter_class = new Intent(this,ItemsAdapter.class);
startActivity(open_adapter_class );
}
}
//this is the error I get
android.content.ActivityNotFoundException: Unable to find explicit activity class
{com.dikolobe.salesagent/com.dikolobe.salesagent.ItemsAdapter}; have you declared this activity in your
AndroidManifest.xml?
I know that am getting this error because adapter is not an activity so I just want to get help on how to
open this adapter class if there is a way to do so
From Android documentation:
An intent is an abstract description of an operation to be performed.
It can be used with startActivity to launch an Activity,
broadcastIntent to send it to any interested BroadcastReceiver
components, and Context.startService(Intent)
So, ItemsAdapter has to be an activity, (eg ...extends AppCompatActivity) so you change from one activity to another.
But what you can do is pass an object from that class to another activity
Intent open_adapter_class = new Intent(this,ItemsAdapter.class);
I was wondering if anyone could tell me how I could check a checkBox in an activity from another activity.
I'm making a homework app and I want to put a check next to the questions that have been completed.
So the first activity has a list of questions and next to them are unchecked boxes. When you click a question, the app takes you to the second activity. In the second activity, I want to check the box of the question that was completed.
You should use SharedPreferences for that. call this in the first activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
prefs.putBoolean("KEY", <your value>).apply();
and something similar in another activity:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean isTicked = prefs.getBoolean("KEY", <default boolean value>);
where KEY can be anything, for example, your question's number
You should use intent and bundle logic for passing data between activities.(in most cases)
In the first activity, whenever you are creating the second activity, you pass your data.
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("checked",checkBox.isChecked());
startActivity(intent);
In the second activity you receive your data by using :
Intent receivedIntent = getIntent();
boolean finalChecked = receivedIntent.getExtras().getBoolean("checked");
//now you can apply your logic with finalChecked variable
You can save checked check question to a bundle and pass it as extra to your second activity through intent and read it from that intent inside your second activity
In your first activity do something like this
public class FirstActivity extends Activity {
public static final String EXTRA_CHECKED_QUESTION = "Checked Question";
// other codes here
private void startSecondActivity() {
String checkedQuestion = getCheckedQuestion(); // a method to get checked question
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra(FirstActivity.EXTRA_CHECKED_QUESTION, checkedQuestion);
startActivity(intent);
}
}
then in your second activity
public class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstance) {
// other codes
String checkedQuestion = getIntent().getStringExtra(FirstActivity.EXTRA_CHECKED_QUESTION);
// do whatever you want with checkQuestion
}
}
Please note that the string which you pass as first parameter to putExtra() method in FirstActivity, should be same as the string you are using with getStringExtra() to retrieve your extra in SecondActivity, otherwise you can not retrieve your passed extra in SecondActivity.
Also, I did not write these codes inside idea, so there might be syntax error
Read more about intent here
I have implemented a 3rd party camera scanning app within a project of mine
I have an adaptor that extends a viewholder so i can have a custom layout
Within a view i have a button
When i create the view i add an OnclickListener to the image which i want to make clickable
i want this OnclickListener to call the camera but i need to call an startActivityForResult
i am confused about the placement of the onActivityResult when calling in this way.
my current attempts have been to create a seperate class(scan_activity) which extends activity but i couldnt get the onclicklistener to start the class
i have then attempted to declare it within the setOnClickListener, which as you can guess also failed.
Im guessing the call to the class is the way forward.im sure its an easy fix, but im not seeing it
please help
thanks in advance
imgCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(context, ScanActivity.class);
intent.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE, 1);
((Activity) context).startActivityForResult(intent,99);
}
}
);
and the class
imgCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new ScanActivity
}
}
);
The first option should work. You create an Intent object and you specify activity component as a parameter. Android will create an Activity object for you, so, never do that manually. Android should manage the lifecycle of activity.
By starting activity for result you tell Android that it should call onActivityResult callback with request code 99 on your first activity when ScanActivity will be closed.
Handling onActivityResult in your Activity:
#Override
public void onActivityResult (int requestCode, int resultCode, Intent data) {
if (requestCode == 99) {
// handle your result here
}
}
I am a newbie to android development, trying to get buttons working. every time i use this code below, the error message "unfortunately the app has stopped". but when i remove the code the app runs but obviously the buttons do nothing. here is the code ive tried
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.ExerciseButton);
button1.setOnClickListener (new View.OnClickListener(){
public void onClick(View v) {
setContentView(R.layout.exercises);
}
});
}
}
anybody able to help me out there? thanks
Don't try to load another View in the current activity. Navigate to a new ExercisesActivity.
Use:
public void onClick(View v) {
Intent intent = new Intent(ExercisesActivity.this, WcActivity.class);
startActivity(intent);
}
You can't call setContentView anymore after the view has loaded (which it obviously has to receive button clicks). Use a different approach, like showing and hiding views or using a ViewFlipper (see Calling setContentView() multiple times), using fragments (see Fragments) or starting a new activity.
Well, from your code, I see a couple of things:
I am usually familiar to using the onClickListener of the Button class if I want to use it for a button. Makes sense, doesn't it?
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
Second thing:
Start a new Activity (if that is what you want) by using an Intent:
Intent myIntent = new Intent(this, exercises.class);
startActivity(myIntent);
You CAN absolutaly call setContentView on any event, even after the view has loaded
I tried your code in a demo project and it is working fine. So, i think the error will be some where in your layout.(Let me know more if you need more help on this)