How do you start a new window activity in Android Studio? - java

I tried several things, such as:
Intent i = new Intent(this, myActivity.class);
startActivity(i)
Where it tells me it cannot resolve symbol 'myActivity'
I tried Context.startActivity and extended the class by Context, at that point it just wants me to implement every single method of Context into my class.
How can I just simply make a new activity visible to the user after an if condition runs into true?

Intent i = new Intent(CurrentActivity.this, myActivity.class);
startActivity(i)
Here myActivity is the name of your second activity (Which you want to open from this intent).
CurrentActivity is the name of your current activity

First you need to understand how to define a class name(Should always start from a capital letter). Then replace the 'myActivity.class' with your new class name.
Make sure that your activity is defined in the AndroidManifest.xml
<activity android:name=".YourActivityName" />
if this doesn't fix the problem then try:
Intent myIntent = new Intent(view.getContext(), MyClass.class);
if view is not found, then implement your class with
public class YourClassName extends AppCompatActivity implements View.OnClickListener{#Override
public void onClick(View v) {
}}
or if you are in a fragment then:
Intent myIntent = new Intent(MyFragment.this.getActivity(), MyClass.class);

Related

Is there a way to create a class to share a transition intent? [building my first android app]

What I am trying to achieve here is create a java class that will allow me to call the method inside the class for an activity transitioning.
For instance, I have 3 activities: Main.xml, A activity.xml, B activity.xml. I want to create a class that I can call from main's java class to transition to A and then from A to B.
I thought maybe it would be somewhere along that lines of:
[Transitioning activity class]
//Behind the scenes for activity transitioning, I don't know how to write this part or if calling it is correct
Intent intent = new Intent(?, ?); startActivity(intent); overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
[Main]
//calling intent from activity transitioning class
intent(this, A_activity.class);
[A Activity]
//calling intent from activity transitioning class
intent(this, B_activity.class);
Also I want to apologize, this is indeed my first rodeo, I am self-taught and have no professional experience in programming or developing. Thank you in advance!
So far what I am currently doing is creating the transitions in each class:
[A activity]
public void onClick(View v){
switch(v.getId()){
case R.id.commBtn:
inqList.clear();
inqAdd.addItem("Commercial");
System.out.println(inqList);
onTransition();
break;
case R.id.resBtn:
inqList.clear();
inqAdd.addItem("Residential");
System.out.println(inqList);
onTransition();
break;
default:
break;
}
}
//Method for transition after clicked
public void onTransition(){
Intent intent = new Intent(this, b_scene.class);
startActivity(intent);
overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
**[Edit]**
///I did this and it works? not sure if its best practice but it worked.
[Transition Class]
public class Transition {
public static void onTrans(Activity activity, Class name) {
Intent intent = new Intent(activity, name);
activity.startActivity(intent);
activity.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
}
[A Activity]
///Just put this in A activity
public void onClick(View v){
...
onTrans(this, B_Activity.class)
}
Sure you can but since that transition class is not an Activity instance (which has the startActivity() method), you need to pass the Activity instance as well, e.g.:
public static void navigateTo(Activity source, Activity destination) {
Intent intent = new Intent(activity, destination.class);
source.startActivity(intent);
source.overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
}
Another common option is to use an abstract BaseActivity that has this method onTransition() and extend your activities from this one (you can also use composition instead of inheritance for a better practice).

Android 11: Cannot come back from startActivityForResult or registerForActivityResult and launch

I have an app with a MainActivity and a SecondaryActivity.
When the MainActivity opens the SecondaryActivity, there is a situation in which I need the user to pick up a phone number from a list that is in the MainActivity.
I've used startActivityForResult to open back the MainActivity with an extra in the intent so that the MainActivity opens directly the fragment with the specific list, like this:
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("UserInfo", userInfo);
intent.putExtra("SecondaryActivity", true);
startActivityForResult(intent, RESULT_FROM_MAIN_ACTIVITY);
And when the user clicks on one element of that list, I set the result and finish the MainActivity in order to return to the SecondaryActivity, like this:
Intent returnIntent = new Intent();
returnIntent.putExtra(PHONE_NUMBER, phoneNumber);
setResult(Activity.RESULT_OK, returnIntent);
finish();
That worked wonderfully until now, but I've noticed that in Android 11 devices, I have 2 problems:
startActivityForResult starts the MainActivity directly in the onResume() function without passing by the onCreate() function, as it is for Android 10 devices and below. And this forces me to check each time the MainActivity's onResume() function is called, if there is any extra sent. Thing that unnecessarily add an extra charge to the already charged MainActivity.
I cannot come back to the SecondaryActivity using finish()! Instead of that, the app is simply closed.
I've tried to use the new Activity Result APIs, and more specifically: registerForActivityResult, like this (in the SecondaryActivity):
private ActivityResultLauncher<Intent> activityResultLauncher;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
manageResult(result.getResultCode(), result.getData());
});
}
public void onPhoneNumberClick() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("UserInfo", userInfo);
intent.putExtra("SecondaryActivity", true);
activityResultLauncher.launch(intent);
}
But, this doesn't change anything to the 2 problems that I described: onResume() is directly called in MainActivity and finish() close the app instead of closing the MainActivity and coming back to the SecondaryActivity. I understand, that the fact that the MainActivity was already opened when I use startActivityForResult or registerForActivityResult and launch can be the cause of those problems, but what can I do to solve them?
Edit:
My MainActivity is defined as singleTask in AndroidManifest and it seems that something changed in Android 11 for the management of activities defined like this. If I remove this tag, it solves the problem but I'm not sure why we defined this activity as singleTask and I don't want to see come back any bug that this was intended to solve...

I want to open Adapter class when I click the imageView but i get ActivityNotFoundException,any alternative method to do so?

#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);

How to pass values to parent tabgroupactivity?

I have one tabGroupActivity. in this tabGroupActivity I start a new activity called "pideInformacion", in this way:
Intent intent = new Intent(getParent(), pideInformacion.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("pideInformacion", intent);
Then, from pideInformacion class the app open a new activity called verMapaGps:
Intent intent = new Intent(this, verMapaGps.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("verMapaGps", intent);
I would like that in verMapaGps set string value in order to return to pideInformacion class.
I see this way to access to parent activity, but I don't know how can I access to parent activity values to set it.
Activity pideInfoActivity = (pideInformacion) getParent();
I can't be able to use startActivityForResult because If I use this method, the tabhost hides and I want to tabbar is show in whole application.
Thanks in advance!
Try to make the string as a static variable and use it in the parent activity.

Java/Android How to call an activity into a method?

How do i call on a seperate activity within a method:
For example:
private void startApp() {
Patient_Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// I want this Button to go to an Detailed_ModeActivity
// This is how i Am doing it right now, but it comes out with an
// error
Intent b = new Intent(this, Detailed_ModeActivity.class);
startActivity(b);
}
});
}
Any Help would be appreciated.
The Button was declared in the onCreate method
Like this:
Intent b = new Intent(v.getContext(), Detailed_ModeActivity.class);
startActivity(b);
The this refers to an View.OnClickListener object which doesn't have startActivity() method and cannot be passed to an Intent. You need to call startActivity() on a Context (e.g. an Activity). Let's say your code is in the MainActivity class. Like this:
Intent b = new Intent(MainActivity.this, Detailed_ModeActivity.class);
MainActivity.this.startActivity(b);
First up, make sure Detailed_ModeActivity extends Activity.
Secondly you need to add the activity class to the manifest.xml file if you haven't already.

Categories